blob: 8d76faee8895f4c15287f0837c9d8a622a1aef06 [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
10#include "../private/GrGLSL.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050012#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070013#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070014#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050015#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070016#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080017#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080018#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070019#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050020#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080021#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050022#include "GrShaderCaps.h"
bsalomonafbf2d62014-09-30 12:18:44 -070023#include "GrSurfacePriv.h"
Robert Phillips3798c862017-03-27 11:08:16 -040024#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070025#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000026#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050027#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000028#include "SkMakeUnique.h"
29#include "SkMipMap.h"
30#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000031#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050032#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000033#include "SkTemplates.h"
34#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050035#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050036#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000037
bsalomon@google.com0b77d682011-08-19 13:28:54 +000038#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000039#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000040
reed@google.comac10a2d2010-12-22 21:39:39 +000041#define SKIP_CACHE_CHECK true
42
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000043#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
44 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
45 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
46 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000048 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
49 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
50 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
51#endif
52
Jim Van Verth32ac83e2016-11-28 15:23:57 -050053//#define USE_NSIGHT
54
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000055///////////////////////////////////////////////////////////////////////////////
56
csmartdaltona7f29642016-07-07 08:49:11 -070057using gr_instanced::InstancedRendering;
58using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070059
Robert Phillipse3302df2017-04-24 07:31:02 -040060using gr_instanced::OpAllocator;
61using gr_instanced::GLOpAllocator;
62
cdalton8917d622015-05-06 13:40:21 -070063static const GrGLenum gXfermodeEquation2Blend[] = {
64 // Basic OpenGL blend equations.
65 GR_GL_FUNC_ADD,
66 GR_GL_FUNC_SUBTRACT,
67 GR_GL_FUNC_REVERSE_SUBTRACT,
68
69 // GL_KHR_blend_equation_advanced.
70 GR_GL_SCREEN,
71 GR_GL_OVERLAY,
72 GR_GL_DARKEN,
73 GR_GL_LIGHTEN,
74 GR_GL_COLORDODGE,
75 GR_GL_COLORBURN,
76 GR_GL_HARDLIGHT,
77 GR_GL_SOFTLIGHT,
78 GR_GL_DIFFERENCE,
79 GR_GL_EXCLUSION,
80 GR_GL_MULTIPLY,
81 GR_GL_HSL_HUE,
82 GR_GL_HSL_SATURATION,
83 GR_GL_HSL_COLOR,
84 GR_GL_HSL_LUMINOSITY
85};
86GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
87GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
88GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
90GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
91GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
92GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
93GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
94GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
95GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
96GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
97GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
98GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
99GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
100GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
101GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
102GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
103GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700104GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700105
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106static const GrGLenum gXfermodeCoeff2Blend[] = {
107 GR_GL_ZERO,
108 GR_GL_ONE,
109 GR_GL_SRC_COLOR,
110 GR_GL_ONE_MINUS_SRC_COLOR,
111 GR_GL_DST_COLOR,
112 GR_GL_ONE_MINUS_DST_COLOR,
113 GR_GL_SRC_ALPHA,
114 GR_GL_ONE_MINUS_SRC_ALPHA,
115 GR_GL_DST_ALPHA,
116 GR_GL_ONE_MINUS_DST_ALPHA,
117 GR_GL_CONSTANT_COLOR,
118 GR_GL_ONE_MINUS_CONSTANT_COLOR,
119 GR_GL_CONSTANT_ALPHA,
120 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000121
122 // extended blend coeffs
123 GR_GL_SRC1_COLOR,
124 GR_GL_ONE_MINUS_SRC1_COLOR,
125 GR_GL_SRC1_ALPHA,
126 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000127};
128
bsalomon861e1032014-12-16 07:33:49 -0800129bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000130 static const bool gCoeffReferencesBlendConst[] = {
131 false,
132 false,
133 false,
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 true,
142 true,
143 true,
144 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000145
146 // extended blend coeffs
147 false,
148 false,
149 false,
150 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000151 };
152 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700153 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000154
bsalomon@google.com47059542012-06-06 20:51:20 +0000155 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
156 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
157 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
158 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
159 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
160 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
161 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
162 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
163 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
164 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
165 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
166 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
167 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
168 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000169
bsalomon@google.com47059542012-06-06 20:51:20 +0000170 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
171 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
172 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
173 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000174
175 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700176 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000177}
178
reed@google.comac10a2d2010-12-22 21:39:39 +0000179///////////////////////////////////////////////////////////////////////////////
180
egdanielff1d5472015-09-10 08:37:20 -0700181
bsalomon682c2692015-05-22 14:01:46 -0700182GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
183 GrContext* context) {
Hal Canary144caf52016-11-07 17:57:18 -0500184 sk_sp<const GrGLInterface> glInterface(
bsalomon424cc262015-05-22 10:37:30 -0700185 reinterpret_cast<const GrGLInterface*>(backendContext));
186 if (!glInterface) {
187 glInterface.reset(GrGLDefaultInterface());
188 } else {
189 glInterface->ref();
190 }
191 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700193 }
Hal Canary144caf52016-11-07 17:57:18 -0500194 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700195 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700196 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700197 }
halcanary96fcdcc2015-08-27 07:41:13 -0700198 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700199}
200
rileya@google.come38160c2012-07-03 18:03:04 +0000201static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000202
bsalomon424cc262015-05-22 10:37:30 -0700203GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000204 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700205 , fGLContext(ctx)
206 , fProgramCache(new ProgramCache(this))
207 , fHWProgramID(0)
208 , fTempSrcFBOID(0)
209 , fTempDstFBOID(0)
210 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700211 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700212 , fHWMinSampleShading(0.0) {
213 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
214 fCopyPrograms[i].fProgram = 0;
215 }
brianosman33f6b3f2016-06-02 05:49:21 -0700216 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
217 fMipmapPrograms[i].fProgram = 0;
218 }
cdaltone2e71c22016-04-07 18:13:29 -0700219 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700220
bsalomon424cc262015-05-22 10:37:30 -0700221 SkASSERT(ctx);
222 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000223
Brian Salomon1edc5b92016-11-29 13:43:46 -0500224 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
225 fHWBoundImageStorages.reset(this->caps()->shaderCaps()->maxCombinedImageStorages());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000226
cdaltone2e71c22016-04-07 18:13:29 -0700227 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
228 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
229 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
230 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
231 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
232 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
233 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
234 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
235 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
236 } else {
237 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
238 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
239 }
240 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
241
cdalton74b8d322016-04-11 14:47:28 -0700242 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500243 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700244 }
245
cdaltone2e71c22016-04-07 18:13:29 -0700246 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
247 fPathRendering.reset(new GrGLPathRendering(this));
248 }
249
bsalomon424cc262015-05-22 10:37:30 -0700250 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000251 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000252 const GrGLubyte* vendor;
253 const GrGLubyte* renderer;
254 const GrGLubyte* version;
255 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
256 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
257 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800258 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000259 this);
tfarina38406c82014-10-31 07:11:12 -0700260 SkDebugf("------ VENDOR %s\n", vendor);
261 SkDebugf("------ RENDERER %s\n", renderer);
262 SkDebugf("------ VERSION %s\n", version);
263 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700264 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700265 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800266 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000267 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000268}
269
bsalomon861e1032014-12-16 07:33:49 -0800270GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700271 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
272 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800273 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700274 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700275 fMipmapProgramArrayBuffer.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700276 fWireRectArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800277
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000278 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000279 // detach the current program so there is no confusion on OpenGL's part
280 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000281 GL_CALL(UseProgram(0));
282 }
283
egdanield803f272015-03-18 13:01:52 -0700284 if (0 != fTempSrcFBOID) {
285 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800286 }
egdanield803f272015-03-18 13:01:52 -0700287 if (0 != fTempDstFBOID) {
288 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800289 }
egdanield803f272015-03-18 13:01:52 -0700290 if (0 != fStencilClearFBOID) {
291 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800292 }
egdaniel0f5f9672015-02-03 11:10:51 -0800293
bsalomon7ea33f52015-11-22 14:51:00 -0800294 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
295 if (0 != fCopyPrograms[i].fProgram) {
296 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
297 }
bsalomon6df86402015-06-01 10:41:49 -0700298 }
bsalomon6dea83f2015-12-03 12:58:06 -0800299
brianosman33f6b3f2016-06-02 05:49:21 -0700300 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
301 if (0 != fMipmapPrograms[i].fProgram) {
302 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
303 }
304 }
305
bsalomon6dea83f2015-12-03 12:58:06 -0800306 if (0 != fWireRectProgram.fProgram) {
307 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
308 }
309
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000310 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700311}
312
bsalomon6e2aad42016-04-01 11:54:31 -0700313void GrGLGpu::disconnect(DisconnectType type) {
314 INHERITED::disconnect(type);
315 if (DisconnectType::kCleanup == type) {
316 if (fHWProgramID) {
317 GL_CALL(UseProgram(0));
318 }
319 if (fTempSrcFBOID) {
320 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
321 }
322 if (fTempDstFBOID) {
323 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
324 }
325 if (fStencilClearFBOID) {
326 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
327 }
bsalomon6e2aad42016-04-01 11:54:31 -0700328 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
329 if (fCopyPrograms[i].fProgram) {
330 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
331 }
332 }
brianosman33f6b3f2016-06-02 05:49:21 -0700333 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
334 if (fMipmapPrograms[i].fProgram) {
335 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
336 }
337 }
bsalomon6e2aad42016-04-01 11:54:31 -0700338 if (fWireRectProgram.fProgram) {
339 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
340 }
bsalomon6e2aad42016-04-01 11:54:31 -0700341 } else {
342 if (fProgramCache) {
343 fProgramCache->abandon();
344 }
345 }
346
347 delete fProgramCache;
348 fProgramCache = nullptr;
349
bsalomonc8dc1f72014-08-21 13:02:13 -0700350 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700351 fTempSrcFBOID = 0;
352 fTempDstFBOID = 0;
353 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700354 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800355 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
356 fCopyPrograms[i].fProgram = 0;
357 }
brianosman33f6b3f2016-06-02 05:49:21 -0700358 fMipmapProgramArrayBuffer.reset();
359 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
360 fMipmapPrograms[i].fProgram = 0;
361 }
bsalomon6dea83f2015-12-03 12:58:06 -0800362 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700363 fWireRectArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700364 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700365 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700366 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000367}
368
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000369///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000370
bsalomon861e1032014-12-16 07:33:49 -0800371void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000372 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000373 if (resetBits & kMisc_GrGLBackendState) {
374 GL_CALL(Disable(GR_GL_DEPTH_TEST));
375 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000376
cdaltone2e71c22016-04-07 18:13:29 -0700377 fHWBufferState[kTexel_GrBufferType].invalidate();
378 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
379 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
380 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700381
robertphillips5fa7f302016-07-21 09:21:04 -0700382 fHWDrawFace = GrDrawFace::kInvalid;
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000383 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500384#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000385 // Desktop-only state that we never change
386 if (!this->glCaps().isCoreProfile()) {
387 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
388 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
389 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
390 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
391 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
392 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
393 }
394 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
395 // core profile. This seems like a bug since the core spec removes any mention of
396 // GL_ARB_imaging.
397 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
398 GL_CALL(Disable(GR_GL_COLOR_TABLE));
399 }
400 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400401
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400402 if (this->caps()->wireframeMode()) {
403 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
404 } else {
405 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
406 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500407#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000408 // Since ES doesn't support glPointSize at all we always use the VS to
409 // set the point size
410 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
411
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000412 }
joshualitt58162332014-08-01 06:44:53 -0700413
414 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700415 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700416 // The arm extension requires specifically enabling MSAA fetching per sample.
417 // On some devices this may have a perf hit. Also multiple render targets are disabled
418 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
419 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000420 fHWWriteToColor = kUnknown_TriState;
421 // we only ever use lines in hairline mode
422 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700423 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000424 }
edisonn@google.comba669992013-06-28 16:03:21 +0000425
egdanielb414f252014-07-29 13:15:47 -0700426 if (resetBits & kMSAAEnable_GrGLBackendState) {
427 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700428
egdanieleed519e2016-01-15 11:36:18 -0800429 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800430 if (0 != this->caps()->maxRasterSamples()) {
431 fHWRasterMultisampleEnabled = kUnknown_TriState;
432 fHWNumRasterSamples = 0;
433 }
434
435 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
436 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700437 GL_CALL(CoverageModulation(GR_GL_RGBA));
438 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000439 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000441 fHWActiveTextureUnitIdx = -1; // invalid
442
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000443 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700444 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500445 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000446 }
cdalton74b8d322016-04-11 14:47:28 -0700447 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
448 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
449 fHWBufferTextures[b].fKnownBound = false;
450 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500451 for (int i = 0; i < fHWBoundImageStorages.count(); ++i) {
Brian Salomon8b7a7dd2016-11-29 15:29:41 -0500452 SkASSERT(this->caps()->shaderCaps()->imageLoadStoreSupport());
Brian Salomonf9f45122016-11-29 11:59:17 -0500453 fHWBoundImageStorages[i].fTextureUniqueID.makeInvalid();
454 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000455 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000456
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000457 if (resetBits & kBlend_GrGLBackendState) {
458 fHWBlendState.invalidate();
459 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000460
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000461 if (resetBits & kView_GrGLBackendState) {
462 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700463 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000464 fHWViewport.invalidate();
465 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000467 if (resetBits & kStencil_GrGLBackendState) {
468 fHWStencilSettings.invalidate();
469 fHWStencilTestEnabled = kUnknown_TriState;
470 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000471
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000472 // Vertex
473 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700474 fHWVertexArrayState.invalidate();
475 fHWBufferState[kVertex_GrBufferType].invalidate();
476 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000477 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000478
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000479 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500480 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700481 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000482 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000483
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000484 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700485 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700486 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000487 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000488 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000489
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000490 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000491 if (resetBits & kPixelStore_GrGLBackendState) {
492 if (this->glCaps().unpackRowLengthSupport()) {
493 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
494 }
495 if (this->glCaps().packRowLengthSupport()) {
496 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
497 }
498 if (this->glCaps().unpackFlipYSupport()) {
499 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
500 }
501 if (this->glCaps().packFlipYSupport()) {
502 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
503 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000504 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000505
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000506 if (resetBits & kProgram_GrGLBackendState) {
507 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000508 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000509}
510
Greg Daniel7ef28f32017-04-20 16:41:55 +0000511sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
512 GrSurfaceOrigin origin,
513 GrBackendTextureFlags flags,
514 int sampleCnt,
bungeman6bd52842016-10-27 09:30:08 -0700515 GrWrapOwnership ownership) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000516 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800517 if (!info || !info->fID) {
518 return nullptr;
519 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000520
Greg Daniel7ef28f32017-04-20 16:41:55 +0000521 // next line relies on GrBackendTextureFlags matching GrTexture's
522 bool renderTarget = SkToBool(flags & kRenderTarget_GrBackendTextureFlag);
Brian Osman766fcbb2017-03-13 09:33:09 -0400523 SkASSERT(!renderTarget || kAdoptAndCache_GrWrapOwnership != ownership); // Not supported
bsalomon7ea33f52015-11-22 14:51:00 -0800524
bsalomonb15b4c12014-10-29 12:41:57 -0700525 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -0800526 idDesc.fInfo = *info;
bsalomone5286e02016-01-14 09:24:09 -0800527
bsalomon7ea33f52015-11-22 14:51:00 -0800528 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
529 if (renderTarget) {
530 // This combination is not supported.
531 return nullptr;
532 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500533 if (!this->caps()->shaderCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800534 return nullptr;
535 }
bsalomone5286e02016-01-14 09:24:09 -0800536 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
537 if (!this->glCaps().rectangleTextureSupport()) {
538 return nullptr;
539 }
540 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
541 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800542 }
bsalomone5286e02016-01-14 09:24:09 -0800543
544 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800545 // for a render buffer that resolves to the texture. We don't support MSAA textures.
Greg Daniel7ef28f32017-04-20 16:41:55 +0000546 if (sampleCnt && !renderTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800547 return nullptr;
548 }
bsalomon10528f12015-10-14 12:54:52 -0700549
Brian Osman766fcbb2017-03-13 09:33:09 -0400550 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000551 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400552 } else {
553 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800554 }
bsalomonb15b4c12014-10-29 12:41:57 -0700555
Ben Wagner18b61f92016-10-25 10:44:02 -0400556 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000557 surfDesc.fFlags = (GrSurfaceFlags) flags;
558 surfDesc.fWidth = backendTex.width();
559 surfDesc.fHeight = backendTex.height();
560 surfDesc.fConfig = backendTex.config();
561 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000562 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
563 // assuming the old behaviour, which is that backend textures are always
564 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
565 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000566 if (kDefault_GrSurfaceOrigin == origin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700567 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000568 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000569 surfDesc.fOrigin = origin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000570 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000571
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000572 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700573 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700574 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700575 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000576 }
bungeman6bd52842016-10-27 09:30:08 -0700577 return GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000578 }
Brian Osman766fcbb2017-03-13 09:33:09 -0400579
580 if (kAdoptAndCache_GrWrapOwnership == ownership) {
581 return sk_sp<GrTexture>(new GrGLTexture(this, SkBudgeted::kYes, surfDesc, idDesc));
582 } else {
583 return GrGLTexture::MakeWrapped(this, surfDesc, idDesc);
584 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000585}
586
Greg Danielbcf612b2017-05-01 13:50:58 +0000587sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
588 GrSurfaceOrigin origin) {
589 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
590 if (!info) {
591 return nullptr;
592 }
593
bsalomonb15b4c12014-10-29 12:41:57 -0700594 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000595 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700596 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700597 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500598 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700599 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000600
bsalomonb15b4c12014-10-29 12:41:57 -0700601 GrSurfaceDesc desc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000602 desc.fConfig = backendRT.config();
senorblanco4b013292015-08-19 09:10:28 -0700603 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000604 desc.fWidth = backendRT.width();
605 desc.fHeight = backendRT.height();
606 desc.fSampleCnt = SkTMin(backendRT.sampleCnt(), this->caps()->maxSampleCount());
607 SkASSERT(kDefault_GrSurfaceOrigin != origin);
608 desc.fOrigin = origin;
bsalomonb15b4c12014-10-29 12:41:57 -0700609
Greg Danielbcf612b2017-05-01 13:50:58 +0000610 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000611}
612
Greg Daniel7ef28f32017-04-20 16:41:55 +0000613sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
614 GrSurfaceOrigin origin,
615 int sampleCnt) {
616 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800617 if (!info || !info->fID) {
618 return nullptr;
619 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800620
jvanverthdb1adf52016-05-03 05:37:45 -0700621 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700622 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800623
jvanverthdb1adf52016-05-03 05:37:45 -0700624 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
625 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800626 // Only texture rectangle and texture 2d are supported. We do not check whether texture
627 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
628 // we assume the necessary support exists.
629 return nullptr;
630 }
631
Ben Wagner18b61f92016-10-25 10:44:02 -0400632 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000633 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
634 surfDesc.fWidth = tex.width();
635 surfDesc.fHeight = tex.height();
636 surfDesc.fConfig = tex.config();
637 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
ericrkf7b8b8a2016-02-24 14:49:51 -0800638 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
639 // assuming the old behaviour, which is that backend textures are always
640 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
641 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000642 if (kDefault_GrSurfaceOrigin == origin) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800643 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
644 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000645 surfDesc.fOrigin = origin;
ericrkf7b8b8a2016-02-24 14:49:51 -0800646 }
647
648 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700649 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800650 return nullptr;
651 }
bungeman6bd52842016-10-27 09:30:08 -0700652 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800653}
654
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000655////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800656
bsalomonf0674512015-07-28 13:26:15 -0700657bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800658 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700659 DrawPreference* drawPreference,
660 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Osmand0be1ef2017-01-11 16:57:15 -0500661 if (GrPixelConfigIsCompressed(dstSurface->config())) {
bsalomonf0674512015-07-28 13:26:15 -0700662 return false;
663 }
664
bsalomon6cb3cbe2015-07-30 07:34:27 -0700665 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
666 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
667 if (!dstSurface->asTexture()) {
668 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800669 } else {
670 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800671 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
672 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800673 return false;
674 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700675 }
676
Brian Osman33910292017-04-18 14:38:53 -0400677 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
678 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
679 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
680 }
681
bsalomon16921ec2015-07-30 15:34:56 -0700682 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
683 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
684 }
685
bsalomon6c9cd552016-01-22 07:17:34 -0800686 // Start off assuming no swizzling
687 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
688 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700689
690 // These settings we will always want if a temp draw is performed. Initially set the config
691 // to srcConfig, though that may be modified if we decide to do a R/G swap.
692 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
693 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
694 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
695 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
696 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
697 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
698
699 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
700
701 if (configsAreRBSwaps) {
702 if (!this->caps()->isConfigTexturable(srcConfig)) {
703 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
704 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800705 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
706 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700707 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
708 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700709 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
710 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800711 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
712 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700713 } else if (kGLES_GrGLStandard == this->glStandard() &&
714 this->glCaps().bgraIsInternalFormat()) {
715 // The internal format and external formats must match texture uploads so we can't
716 // swizzle while uploading when BGRA is a distinct internal format.
717 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
718 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800719 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
720 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700721 }
722 }
723
724 if (!this->glCaps().unpackFlipYSupport() &&
725 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
726 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
727 }
728
729 return true;
730}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000731
jvanverth17aa0472016-01-05 10:41:27 -0800732static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
733 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700734 if (!glTex) {
735 return false;
736 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000737
bsalomon16921ec2015-07-30 15:34:56 -0700738 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
739 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
740 return false;
741 }
742
bsalomone5286e02016-01-14 09:24:09 -0800743 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
744 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800745 return false;
746 }
747
jvanverth17aa0472016-01-05 10:41:27 -0800748 return true;
749}
750
751bool GrGLGpu::onWritePixels(GrSurface* surface,
752 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800753 GrPixelConfig config,
754 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800755 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
756
757 if (!check_write_and_transfer_input(glTex, surface, config)) {
758 return false;
759 }
760
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000761 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700762 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000763
krajcevski145d48c2014-06-11 16:07:50 -0700764 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700765 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800766 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700767 SkASSERT(config == glTex->desc().fConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800768 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800769 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700770 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800771 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800772 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700773 }
774
cblume55f2d2d2016-02-26 13:20:48 -0800775 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000776}
777
jvanverthc3d706f2016-04-20 10:33:27 -0700778bool GrGLGpu::onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800779 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700780 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800781 size_t offset, size_t rowBytes) {
jvanverthc3d706f2016-04-20 10:33:27 -0700782 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800783
jvanverthc3d706f2016-04-20 10:33:27 -0700784 if (!check_write_and_transfer_input(glTex, surface, config)) {
jvanverth17aa0472016-01-05 10:41:27 -0800785 return false;
786 }
787
788 // For the moment, can't transfer compressed data
789 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
790 return false;
791 }
792
793 this->setScratchTextureUnit();
794 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
795
cdalton397536c2016-03-25 12:15:03 -0700796 SkASSERT(!transferBuffer->isMapped());
797 SkASSERT(!transferBuffer->isCPUBacked());
798 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700799 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800800
801 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800802 GrMipLevel mipLevel;
jvanverthc3d706f2016-04-20 10:33:27 -0700803 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800804 mipLevel.fRowBytes = rowBytes;
805 SkSTArray<1, GrMipLevel> texels;
806 texels.push_back(mipLevel);
jcgregorioe7d7f902016-02-04 19:51:25 -0800807 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800808 left, top, width, height, config, texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700809 return success;
jvanverth17aa0472016-01-05 10:41:27 -0800810}
811
bsalomonf46a1242015-12-15 12:37:38 -0800812// For GL_[UN]PACK_ALIGNMENT.
813static inline GrGLint config_alignment(GrPixelConfig config) {
814 SkASSERT(!GrPixelConfigIsCompressed(config));
815 switch (config) {
816 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500817 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800818 return 1;
819 case kRGB_565_GrPixelConfig:
820 case kRGBA_4444_GrPixelConfig:
821 case kAlpha_half_GrPixelConfig:
822 case kRGBA_half_GrPixelConfig:
823 return 2;
824 case kRGBA_8888_GrPixelConfig:
825 case kBGRA_8888_GrPixelConfig:
826 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700827 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500828 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800829 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500830 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800831 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500832 case kUnknown_GrPixelConfig:
833 case kETC1_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800834 return 0;
835 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500836 SkFAIL("Invalid pixel config");
837 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800838}
839
bsalomonb15b4c12014-10-29 12:41:57 -0700840static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
841 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700842 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000843 return GR_GL_GET_ERROR(interface);
844 } else {
845 return CHECK_ALLOC_ERROR(interface);
846 }
847}
848
cblume55f2d2d2016-02-26 13:20:48 -0800849/**
850 * Creates storage space for the texture and fills it with texels.
851 *
852 * @param desc The surface descriptor for the texture being created.
853 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800854 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700855 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
856 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800857 * @param externalFormat The data format used for the external storage of the texture.
858 * @param externalType The type of the data used for the external storage of the texture.
859 * @param texels The texel data of the texture being created.
860 * @param baseWidth The width of the texture's base mipmap level
861 * @param baseHeight The height of the texture's base mipmap level
862 * @param succeeded Set to true if allocating and populating the texture completed
863 * without error.
864 */
bsalomone699d0c2016-03-09 06:25:15 -0800865static bool allocate_and_populate_uncompressed_texture(const GrSurfaceDesc& desc,
cblume55f2d2d2016-02-26 13:20:48 -0800866 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800867 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800868 GrGLenum target,
869 GrGLenum internalFormat,
brianosman81a84852016-09-12 09:05:14 -0700870 GrGLenum internalFormatForTexStorage,
cblume55f2d2d2016-02-26 13:20:48 -0800871 GrGLenum externalFormat,
872 GrGLenum externalType,
873 const SkTArray<GrMipLevel>& texels,
bsalomone699d0c2016-03-09 06:25:15 -0800874 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800875 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800876
877 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
878 // We can only use TexStorage if we know we will not later change the storage requirements.
879 // This means if we may later want to add mipmaps, we cannot use TexStorage.
880 // Right now, we cannot know if we will later add mipmaps or not.
881 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500882 // mipmaps or are using a format incompatible with MIP maps.
883 useTexStorage &= texels.count() > 1 || GrPixelConfigIsSint(desc.fConfig);
cblume790d5132016-02-29 11:13:29 -0800884
885 if (useTexStorage) {
886 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800887 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -0800888 TexStorage2D(target,
Brian Salomonbf7b6202016-11-11 16:08:03 -0500889 SkTMax(texels.count(), 1),
brianosman81a84852016-09-12 09:05:14 -0700890 internalFormatForTexStorage,
cblume790d5132016-02-29 11:13:29 -0800891 desc.fWidth, desc.fHeight));
cblume55f2d2d2016-02-26 13:20:48 -0800892 GrGLenum error = check_alloc_error(desc, &interface);
893 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800894 return false;
cblume790d5132016-02-29 11:13:29 -0800895 } else {
896 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
897 const void* currentMipData = texels[currentMipLevel].fPixels;
898 if (currentMipData == nullptr) {
899 continue;
900 }
901 int twoToTheMipLevel = 1 << currentMipLevel;
902 int currentWidth = SkTMax(1, desc.fWidth / twoToTheMipLevel);
903 int currentHeight = SkTMax(1, desc.fHeight / twoToTheMipLevel);
904
905 GR_GL_CALL(&interface,
906 TexSubImage2D(target,
907 currentMipLevel,
908 0, // left
909 0, // top
910 currentWidth,
911 currentHeight,
912 externalFormat, externalType,
913 currentMipData));
914 }
bsalomone699d0c2016-03-09 06:25:15 -0800915 return true;
cblume790d5132016-02-29 11:13:29 -0800916 }
917 } else {
bsalomone699d0c2016-03-09 06:25:15 -0800918 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -0800919 GL_ALLOC_CALL(&interface,
920 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800921 0,
cblume790d5132016-02-29 11:13:29 -0800922 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800923 baseWidth,
924 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800925 0, // border
926 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800927 nullptr));
cblume790d5132016-02-29 11:13:29 -0800928 GrGLenum error = check_alloc_error(desc, &interface);
929 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800930 return false;
931 }
932 } else {
933 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
934 int twoToTheMipLevel = 1 << currentMipLevel;
935 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
936 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
937 const void* currentMipData = texels[currentMipLevel].fPixels;
938 // Even if curremtMipData is nullptr, continue to call TexImage2D.
939 // This will allocate texture memory which we can later populate.
940 GL_ALLOC_CALL(&interface,
941 TexImage2D(target,
942 currentMipLevel,
943 internalFormat,
944 currentWidth,
945 currentHeight,
946 0, // border
947 externalFormat, externalType,
948 currentMipData));
949 GrGLenum error = check_alloc_error(desc, &interface);
950 if (error != GR_GL_NO_ERROR) {
951 return false;
952 }
cblume790d5132016-02-29 11:13:29 -0800953 }
cblume55f2d2d2016-02-26 13:20:48 -0800954 }
955 }
bsalomone699d0c2016-03-09 06:25:15 -0800956 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800957}
958
959/**
960 * Creates storage space for the texture and fills it with texels.
961 *
962 * @param desc The surface descriptor for the texture being created.
963 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800964 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -0800965 * @param internalFormat The data format used for the internal storage of the texture.
966 * @param texels The texel data of the texture being created.
cblume55f2d2d2016-02-26 13:20:48 -0800967 */
968static bool allocate_and_populate_compressed_texture(const GrSurfaceDesc& desc,
969 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800970 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800971 GrGLenum target, GrGLenum internalFormat,
972 const SkTArray<GrMipLevel>& texels,
973 int baseWidth, int baseHeight) {
974 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800975
cblume790d5132016-02-29 11:13:29 -0800976 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
977 // We can only use TexStorage if we know we will not later change the storage requirements.
978 // This means if we may later want to add mipmaps, we cannot use TexStorage.
979 // Right now, we cannot know if we will later add mipmaps or not.
980 // The only time we can use TexStorage is when we already have the
981 // mipmaps.
982 useTexStorage &= texels.count() > 1;
cblume55f2d2d2016-02-26 13:20:48 -0800983
cblume790d5132016-02-29 11:13:29 -0800984 if (useTexStorage) {
985 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800986 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -0800987 TexStorage2D(target,
988 texels.count(),
989 internalFormat,
990 baseWidth, baseHeight));
cblume55f2d2d2016-02-26 13:20:48 -0800991 GrGLenum error = check_alloc_error(desc, &interface);
992 if (error != GR_GL_NO_ERROR) {
993 return false;
cblume790d5132016-02-29 11:13:29 -0800994 } else {
995 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
996 const void* currentMipData = texels[currentMipLevel].fPixels;
997 if (currentMipData == nullptr) {
998 continue;
999 }
1000
1001 int twoToTheMipLevel = 1 << currentMipLevel;
1002 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1003 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1004
1005 // Make sure that the width and height that we pass to OpenGL
1006 // is a multiple of the block size.
1007 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1008 currentHeight);
1009 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1010 currentMipLevel,
1011 0, // left
1012 0, // top
1013 currentWidth,
1014 currentHeight,
1015 internalFormat,
1016 SkToInt(dataSize),
1017 currentMipData));
1018 }
1019 }
1020 } else {
1021 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1022 int twoToTheMipLevel = 1 << currentMipLevel;
1023 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1024 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1025
1026 // Make sure that the width and height that we pass to OpenGL
1027 // is a multiple of the block size.
1028 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, baseWidth, baseHeight);
1029
1030 GL_ALLOC_CALL(&interface,
1031 CompressedTexImage2D(target,
1032 currentMipLevel,
1033 internalFormat,
1034 currentWidth,
1035 currentHeight,
1036 0, // border
1037 SkToInt(dataSize),
1038 texels[currentMipLevel].fPixels));
1039
1040 GrGLenum error = check_alloc_error(desc, &interface);
1041 if (error != GR_GL_NO_ERROR) {
1042 return false;
1043 }
cblume55f2d2d2016-02-26 13:20:48 -08001044 }
1045 }
1046
1047 return true;
1048}
1049
1050/**
1051 * After a texture is created, any state which was altered during its creation
1052 * needs to be restored.
1053 *
1054 * @param interface The GL interface to use.
1055 * @param caps The capabilities of the GL device.
1056 * @param restoreGLRowLength Should the row length unpacking be restored?
1057 * @param glFlipY Did GL flip the texture vertically?
1058 */
1059static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1060 bool restoreGLRowLength, bool glFlipY) {
1061 if (restoreGLRowLength) {
1062 SkASSERT(caps.unpackRowLengthSupport());
1063 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1064 }
1065 if (glFlipY) {
1066 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1067 }
1068}
1069
bsalomon861e1032014-12-16 07:33:49 -08001070bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -08001071 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -08001072 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001073 int left, int top, int width, int height,
1074 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001075 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001076 // If we're uploading compressed data then we should be using uploadCompressedTexData
1077 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1078
bsalomon5b30c6f2015-12-17 14:17:34 -08001079 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
1080
cblume55f2d2d2016-02-26 13:20:48 -08001081 // texels is const.
1082 // But we may need to flip the texture vertically to prepare it.
1083 // Rather than flip in place and alter the incoming data,
1084 // we allocate a new buffer to flip into.
1085 // This means we need to make a non-const shallow copy of texels.
1086 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1087
1088 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1089 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -08001090 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001091 }
1092
1093 const GrGLInterface* interface = this->glInterface();
1094 const GrGLCaps& caps = this->glCaps();
1095
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001096 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001097
1098 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001099 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001100 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001101
cblume55f2d2d2016-02-26 13:20:48 -08001102 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1103 int twoToTheMipLevel = 1 << currentMipLevel;
1104 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1105 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1106
cblume55f2d2d2016-02-26 13:20:48 -08001107 if (currentHeight > SK_MaxS32 ||
1108 currentWidth > SK_MaxS32) {
1109 return false;
1110 }
1111 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
1112 &currentWidth,
1113 &currentHeight,
1114 &texelsShallowCopy[currentMipLevel].fPixels,
1115 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
1116 return false;
1117 }
1118 if (currentWidth < 0 || currentHeight < 0) {
1119 return false;
1120 }
1121 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001122
bsalomon5b30c6f2015-12-17 14:17:34 -08001123 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001124 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001125 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001126 GrGLenum externalFormat;
1127 GrGLenum externalType;
1128 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
1129 &externalFormat, &externalType)) {
1130 return false;
1131 }
brianosman81a84852016-09-12 09:05:14 -07001132 // TexStorage requires a sized format, and internalFormat may or may not be
1133 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(desc.fConfig);
1134
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001135 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001136 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001137 * because our srcData has extra bytes past each row. If so, we need
1138 * to trim those off here, since GL ES may not let us specify
1139 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001140 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001141 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001142 bool swFlipY = false;
1143 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001144
bsalomone699d0c2016-03-09 06:25:15 -08001145 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001146 if (caps.unpackFlipYSupport()) {
1147 glFlipY = true;
1148 } else {
1149 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001150 }
cblume55f2d2d2016-02-26 13:20:48 -08001151 }
1152
1153 // in case we need a temporary, trimmed copy of the src pixels
1154 SkAutoSMalloc<128 * 128> tempStorage;
1155
1156 // find the combined size of all the mip levels and the relative offset of
1157 // each into the collective buffer
1158 size_t combined_buffer_size = 0;
1159 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1160 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1161 int twoToTheMipLevel = 1 << currentMipLevel;
1162 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1163 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1164 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1165 individual_mip_offsets.push_back(combined_buffer_size);
1166 combined_buffer_size += trimmedSize;
1167 }
1168 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1169
1170 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001171 int twoToTheMipLevel = 1 << currentMipLevel;
1172 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1173 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1174 const size_t trimRowBytes = currentWidth * bpp;
1175
1176 /*
1177 * check whether to allocate a temporary buffer for flipping y or
1178 * because our srcData has extra bytes past each row. If so, we need
1179 * to trim those off here, since GL ES may not let us specify
1180 * GL_UNPACK_ROW_LENGTH.
1181 */
1182 restoreGLRowLength = false;
1183
1184 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
ericrk154349b2016-05-10 14:36:53 -07001185
1186 // TODO: This optimization should be enabled with or without mips.
1187 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1188 // mip level, before calling glTexImage2D.
1189 const bool usesMips = texelsShallowCopy.count() > 1;
1190 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001191 // can't use this for flipping, only non-neg values allowed. :(
1192 if (rowBytes != trimRowBytes) {
1193 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001194 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001195 restoreGLRowLength = true;
1196 }
jvanverth17aa0472016-01-05 10:41:27 -08001197 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001198 if (trimRowBytes != rowBytes || swFlipY) {
1199 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001200 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1201 if (swFlipY && currentHeight >= 1) {
1202 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001203 }
cblume55f2d2d2016-02-26 13:20:48 -08001204 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1205 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001206 memcpy(dst, src, trimRowBytes);
1207 if (swFlipY) {
1208 src -= rowBytes;
1209 } else {
1210 src += rowBytes;
1211 }
1212 dst += trimRowBytes;
1213 }
1214 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001215 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1216 individual_mip_offsets[currentMipLevel];
1217 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001218 }
jvanverth17aa0472016-01-05 10:41:27 -08001219 } else {
1220 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001221 }
bsalomone699d0c2016-03-09 06:25:15 -08001222 }
1223
1224 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001225 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001226 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001227 }
cblume55f2d2d2016-02-26 13:20:48 -08001228 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT,
1229 config_alignment(desc.fConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001230 }
cblume55f2d2d2016-02-26 13:20:48 -08001231
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001232 bool succeeded = true;
cblume55f2d2d2016-02-26 13:20:48 -08001233 if (kNewTexture_UploadType == uploadType &&
1234 0 == left && 0 == top &&
bsalomon7e68ab72016-04-13 14:29:25 -07001235 desc.fWidth == width && desc.fHeight == height) {
bsalomone699d0c2016-03-09 06:25:15 -08001236 succeeded = allocate_and_populate_uncompressed_texture(desc, *interface, caps, target,
brianosman81a84852016-09-12 09:05:14 -07001237 internalFormat,
1238 internalFormatForTexStorage,
1239 externalFormat, externalType,
1240 texelsShallowCopy, width, height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001241 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001242 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001243 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001244 }
cblume55f2d2d2016-02-26 13:20:48 -08001245 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1246 currentMipLevel++) {
1247 int twoToTheMipLevel = 1 << currentMipLevel;
1248 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1249 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001250
1251 GL_CALL(TexSubImage2D(target,
1252 currentMipLevel,
1253 left, top,
1254 currentWidth,
1255 currentHeight,
1256 externalFormat, externalType,
1257 texelsShallowCopy[currentMipLevel].fPixels));
1258 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001259 }
1260
cblume55f2d2d2016-02-26 13:20:48 -08001261 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1262
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001263 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001264}
1265
krajcevski145d48c2014-06-11 16:07:50 -07001266// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001267// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001268// create a CompressedTexData struct that takes a desc/ptr and figures out
1269// the proper upload semantics. Then users can construct this function how they
1270// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -08001271bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -07001272 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -08001273 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -08001274 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001275 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -08001276 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
krajcevski9c0e6292014-06-02 07:38:14 -07001277
1278 // No support for software flip y, yet...
1279 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
1280
cblume55f2d2d2016-02-26 13:20:48 -08001281 const GrGLInterface* interface = this->glInterface();
1282 const GrGLCaps& caps = this->glCaps();
1283
krajcevski145d48c2014-06-11 16:07:50 -07001284 if (-1 == width) {
1285 width = desc.fWidth;
1286 }
1287#ifdef SK_DEBUG
1288 else {
1289 SkASSERT(width <= desc.fWidth);
1290 }
1291#endif
1292
1293 if (-1 == height) {
1294 height = desc.fHeight;
1295 }
1296#ifdef SK_DEBUG
1297 else {
1298 SkASSERT(height <= desc.fHeight);
1299 }
1300#endif
1301
bsalomon76148af2016-01-12 11:13:47 -08001302 // We only need the internal format for compressed 2D textures.
1303 GrGLenum internalFormat;
cblume55f2d2d2016-02-26 13:20:48 -08001304 if (!caps.getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001305 return false;
1306 }
krajcevski9c0e6292014-06-02 07:38:14 -07001307
jvanverth17aa0472016-01-05 10:41:27 -08001308 if (kNewTexture_UploadType == uploadType) {
cblume790d5132016-02-29 11:13:29 -08001309 return allocate_and_populate_compressed_texture(desc, *interface, caps, target,
1310 internalFormat, texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001311 } else {
cblume55f2d2d2016-02-26 13:20:48 -08001312 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001313 SkASSERT(texels[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001314
1315 int twoToTheMipLevel = 1 << currentMipLevel;
1316 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1317 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1318
1319 // Make sure that the width and height that we pass to OpenGL
1320 // is a multiple of the block size.
1321 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1322 currentHeight);
1323 GL_CALL(CompressedTexSubImage2D(target,
1324 currentMipLevel,
1325 left, top,
1326 currentWidth,
1327 currentHeight,
1328 internalFormat,
1329 SkToInt(dataSize),
1330 texels[currentMipLevel].fPixels));
1331 }
krajcevski145d48c2014-06-11 16:07:50 -07001332 }
krajcevski9c0e6292014-06-02 07:38:14 -07001333
bsalomond4cb9222014-08-11 14:19:09 -07001334 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001335}
1336
bsalomon424cc262015-05-22 10:37:30 -07001337static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001338 int sampleCount,
1339 GrGLenum format,
1340 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001341 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001342 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001343 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001344 case GrGLCaps::kEXT_MSFBOType:
1345 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001346 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001347 GL_ALLOC_CALL(ctx.interface(),
1348 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1349 sampleCount,
1350 format,
1351 width, height));
1352 break;
1353 case GrGLCaps::kES_Apple_MSFBOType:
1354 GL_ALLOC_CALL(ctx.interface(),
1355 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1356 sampleCount,
1357 format,
1358 width, height));
1359 break;
1360 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1361 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1362 GL_ALLOC_CALL(ctx.interface(),
1363 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1364 sampleCount,
1365 format,
1366 width, height));
1367 break;
1368 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001369 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001370 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001371 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001372 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001373}
1374
egdanielb0e1be22015-04-22 13:27:39 -07001375bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001376 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001377 GrGLRenderTarget::IDDesc* idDesc) {
1378 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001379 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001380 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001381 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001382 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1383 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001384 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001385
1386 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001387
bsalomona11e5fc2015-12-18 07:59:41 -08001388 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001389
bsalomonb15b4c12014-10-29 12:41:57 -07001390 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001391 goto FAILED;
1392 }
1393
egdanield803f272015-03-18 13:01:52 -07001394 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1395 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001396 goto FAILED;
1397 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001398
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001399 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1400 // the texture bound to the other. The exception is the IMG multisample extension. With this
1401 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1402 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001403 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001404 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001405 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001406 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001407 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001408 goto FAILED;
1409 }
bsalomon76148af2016-01-12 11:13:47 -08001410 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1411 return false;
1412 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001413 } else {
egdanield803f272015-03-18 13:01:52 -07001414 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001415 }
1416
egdanield803f272015-03-18 13:01:52 -07001417 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001418 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001419 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001420 SkASSERT(desc.fSampleCnt > 0);
1421 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001422 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001423 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001424 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001425 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001426 goto FAILED;
1427 }
egdanield803f272015-03-18 13:01:52 -07001428 fStats.incRenderTargetBinds();
1429 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1430 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001431 GR_GL_COLOR_ATTACHMENT0,
1432 GR_GL_RENDERBUFFER,
1433 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001434 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1435 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001436 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1437 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1438 goto FAILED;
1439 }
bsalomon424cc262015-05-22 10:37:30 -07001440 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001441 }
1442 }
egdanield803f272015-03-18 13:01:52 -07001443 fStats.incRenderTargetBinds();
1444 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001445
bsalomonb15b4c12014-10-29 12:41:57 -07001446 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001447 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001448 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001449 texInfo.fTarget,
1450 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001451 } else {
egdanield803f272015-03-18 13:01:52 -07001452 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001453 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001454 texInfo.fTarget,
1455 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001456 }
bsalomonb15b4c12014-10-29 12:41:57 -07001457 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1458 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001459 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001460 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1461 goto FAILED;
1462 }
bsalomon424cc262015-05-22 10:37:30 -07001463 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001464 }
1465
1466 return true;
1467
1468FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001469 if (idDesc->fMSColorRenderbufferID) {
1470 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001471 }
egdanield803f272015-03-18 13:01:52 -07001472 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1473 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001474 }
egdanield803f272015-03-18 13:01:52 -07001475 if (idDesc->fTexFBOID) {
1476 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001477 }
1478 return false;
1479}
1480
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001481// good to set a break-point here to know when createTexture fails
1482static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001483// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001484 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001485}
1486
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001487#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001488static size_t as_size_t(int x) {
1489 return x;
1490}
1491#endif
1492
kkinnunen2e6055b2016-04-22 01:48:29 -07001493static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface) {
cblume55f2d2d2016-02-26 13:20:48 -08001494 GrGLTexture::IDDesc idDesc;
1495 idDesc.fInfo.fID = 0;
1496 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
kkinnunen2e6055b2016-04-22 01:48:29 -07001497 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
cblume55f2d2d2016-02-26 13:20:48 -08001498 // When we create the texture, we only
1499 // create GL_TEXTURE_2D at the moment.
1500 // External clients can do something different.
1501 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1502 return idDesc;
1503}
1504
1505static void set_initial_texture_params(const GrGLInterface* interface,
1506 const GrGLTextureInfo& info,
1507 GrGLTexture::TexParams* initialTexParams) {
1508 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1509 // drivers have a bug where an FBO won't be complete if it includes a
1510 // texture that is not mipmap complete (considering the filter in use).
1511 // we only set a subset here so invalidate first
1512 initialTexParams->invalidate();
1513 initialTexParams->fMinFilter = GR_GL_NEAREST;
1514 initialTexParams->fMagFilter = GR_GL_NEAREST;
1515 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1516 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1517 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1518 GR_GL_TEXTURE_MAG_FILTER,
1519 initialTexParams->fMagFilter));
1520 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1521 GR_GL_TEXTURE_MIN_FILTER,
1522 initialTexParams->fMinFilter));
1523 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1524 GR_GL_TEXTURE_WRAP_S,
1525 initialTexParams->fWrapS));
1526 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1527 GR_GL_TEXTURE_WRAP_T,
1528 initialTexParams->fWrapT));
1529}
1530
egdanielb0e1be22015-04-22 13:27:39 -07001531GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001532 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001533 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001534 // We fail if the MSAA was requested and is not available.
1535 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001536 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001537 return return_null_texture();
1538 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001539
bsalomonf2703d82014-10-28 14:33:06 -07001540 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001541
bsalomonb15b4c12014-10-29 12:41:57 -07001542 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001543 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001544 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001545 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001546 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001547 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001548
Robert Phillipsd6214d42016-11-07 08:23:48 -05001549 bool wasMipMapDataProvided = false;
1550 if (texels.count() > 1) {
1551 wasMipMapDataProvided = true;
1552 }
1553
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001554 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001556 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001557 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001558 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001559
kkinnunen2e6055b2016-04-22 01:48:29 -07001560 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001561 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001562 return return_null_texture();
1563 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001564 tex = new GrGLTextureRenderTarget(this, budgeted, desc, idDesc, rtIDDesc,
1565 wasMipMapDataProvided);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001566 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -07001567 tex = new GrGLTexture(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001568 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001569 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001570#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001571 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001572 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001573#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001574 return tex;
1575}
1576
egdanielb0e1be22015-04-22 13:27:39 -07001577GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001578 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001579 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001580 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001581 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001582 return return_null_texture();
1583 }
1584
kkinnunen2e6055b2016-04-22 01:48:29 -07001585 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface());
bsalomon091f60c2015-11-10 11:54:56 -08001586 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001587 return return_null_texture();
1588 }
1589
1590 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001591 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001592
krajcevski9c0e6292014-06-02 07:38:14 -07001593 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001594 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001595
cblume55f2d2d2016-02-26 13:20:48 -08001596 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001597 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001598 return return_null_texture();
1599 }
1600
1601 GrGLTexture* tex;
kkinnunen2e6055b2016-04-22 01:48:29 -07001602 tex = new GrGLTexture(this, budgeted, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001603 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1604#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001605 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001606 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001607#endif
1608 return tex;
1609}
1610
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001611namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001612
egdaniel8dc7c3a2015-04-16 11:22:42 -07001613const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001614
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001615void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001616 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001617
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001618 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001619 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001620 (kUnknownBitCount == format->fTotalBits));
1621 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001622 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001623 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1624 (GrGLint*)&format->fStencilBits);
1625 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001626 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001627 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1628 (GrGLint*)&format->fTotalBits);
1629 format->fTotalBits += format->fStencilBits;
1630 } else {
1631 format->fTotalBits = format->fStencilBits;
1632 }
1633 }
1634}
1635}
1636
egdanielff1d5472015-09-10 08:37:20 -07001637int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001638 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001639 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001640 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1641 // Default to unsupported, set this if we find a stencil format that works.
1642 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001643 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001644 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001645 GL_CALL(GenTextures(1, &colorID));
1646 this->setScratchTextureUnit();
1647 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1648 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1649 GR_GL_TEXTURE_MAG_FILTER,
1650 GR_GL_NEAREST));
1651 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1652 GR_GL_TEXTURE_MIN_FILTER,
1653 GR_GL_NEAREST));
1654 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1655 GR_GL_TEXTURE_WRAP_S,
1656 GR_GL_CLAMP_TO_EDGE));
1657 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1658 GR_GL_TEXTURE_WRAP_T,
1659 GR_GL_CLAMP_TO_EDGE));
1660
bsalomon76148af2016-01-12 11:13:47 -08001661 GrGLenum internalFormat;
1662 GrGLenum externalFormat;
1663 GrGLenum externalType;
1664 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1665 &externalType)) {
1666 return false;
1667 }
egdanielff1d5472015-09-10 08:37:20 -07001668 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1669 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001670 0,
bsalomon76148af2016-01-12 11:13:47 -08001671 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001672 kSize,
1673 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001674 0,
bsalomon76148af2016-01-12 11:13:47 -08001675 externalFormat,
1676 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001677 NULL));
bsalomon30447372015-12-21 09:03:05 -08001678 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001679 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001680 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001681 }
1682
1683 // unbind the texture from the texture unit before binding it to the frame buffer
1684 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1685
1686 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001687 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001688 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001689 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001690 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001691 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1692 GR_GL_COLOR_ATTACHMENT0,
1693 GR_GL_TEXTURE_2D,
1694 colorID,
1695 0));
bsalomon30447372015-12-21 09:03:05 -08001696 GrGLuint sbRBID = 0;
1697 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001698
1699 // look over formats till I find a compatible one
1700 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001701 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001702 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001703 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1704 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1705 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1706 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1707 sFmt.fInternalFormat,
1708 kSize, kSize));
1709 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001710 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001711 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001712 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001713 if (sFmt.fPacked) {
1714 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1715 GR_GL_DEPTH_ATTACHMENT,
1716 GR_GL_RENDERBUFFER, sbRBID));
1717 } else {
1718 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1719 GR_GL_DEPTH_ATTACHMENT,
1720 GR_GL_RENDERBUFFER, 0));
1721 }
1722 GrGLenum status;
1723 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1724 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1725 firstWorkingStencilFormatIndex = i;
1726 break;
1727 }
egdanielff1d5472015-09-10 08:37:20 -07001728 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1729 GR_GL_STENCIL_ATTACHMENT,
1730 GR_GL_RENDERBUFFER, 0));
1731 if (sFmt.fPacked) {
1732 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1733 GR_GL_DEPTH_ATTACHMENT,
1734 GR_GL_RENDERBUFFER, 0));
1735 }
egdanielff1d5472015-09-10 08:37:20 -07001736 }
1737 }
bsalomon30447372015-12-21 09:03:05 -08001738 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001739 }
1740 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001741 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1742 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001743 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001744 }
bsalomon30447372015-12-21 09:03:05 -08001745 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001746}
1747
erikchen9a1ed5d2016-02-10 16:32:34 -08001748bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001749 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1750 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001751 info->fID = 0;
1752 info->fTarget = GR_GL_TEXTURE_2D;
1753 GL_CALL(GenTextures(1, &(info->fID)));
1754
1755 if (!info->fID) {
1756 return false;
1757 }
1758
1759 this->setScratchTextureUnit();
1760 GL_CALL(BindTexture(info->fTarget, info->fID));
1761
1762 if (renderTarget && this->glCaps().textureUsageSupport()) {
1763 // provides a hint about how this texture will be used
1764 GL_CALL(TexParameteri(info->fTarget,
1765 GR_GL_TEXTURE_USAGE,
1766 GR_GL_FRAMEBUFFER_ATTACHMENT));
1767 }
1768
cblume55f2d2d2016-02-26 13:20:48 -08001769 if (info) {
1770 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1771 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001772 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1773 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001774 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001775 GL_CALL(DeleteTextures(1, &(info->fID)));
1776 return false;
1777 }
1778 return true;
1779}
1780
egdanielec00d942015-09-14 12:56:10 -07001781GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1782 int width,
1783 int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001784 SkASSERT(width >= rt->width());
1785 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001786
vbuzinovdded6962015-06-12 08:59:45 -07001787 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001788 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001789
egdanielff1d5472015-09-10 08:37:20 -07001790 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001791 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001792 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001793 }
egdanielff1d5472015-09-10 08:37:20 -07001794
1795 if (!sbDesc.fRenderbufferID) {
1796 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1797 }
1798 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001799 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001800 }
1801 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1802 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1803 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1804 // we do this "if" so that we don't call the multisample
1805 // version on a GL that doesn't have an MSAA extension.
1806 if (samples > 0) {
1807 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1808 samples,
1809 sFmt.fInternalFormat,
1810 width, height));
1811 } else {
1812 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1813 sFmt.fInternalFormat,
1814 width, height));
1815 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1816 }
1817 fStats.incStencilAttachmentCreates();
1818 // After sized formats we attempt an unsized format and take
1819 // whatever sizes GL gives us. In that case we query for the size.
1820 GrGLStencilAttachment::Format format = sFmt;
1821 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001822 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1823 sbDesc,
1824 width,
1825 height,
1826 samples,
1827 format);
1828 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001829}
1830
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001831////////////////////////////////////////////////////////////////////////////////
1832
jvanverth73063dc2015-12-03 09:15:47 -08001833// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1834// objects are implemented as client-side-arrays on tile-deferred architectures.
1835#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1836
cdaltone2e71c22016-04-07 18:13:29 -07001837GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001838 GrAccessPattern accessPattern, const void* data) {
1839 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001840}
1841
Robert Phillipse3302df2017-04-24 07:31:02 -04001842
1843std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1844 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1845}
1846
csmartdaltone0d36292016-07-29 08:14:20 -07001847InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1848 return new GLInstancedRendering(this);
1849}
1850
bsalomon3e791242014-12-17 13:43:13 -08001851void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001852 const GrGLIRect& rtViewport,
1853 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001854 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001855 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001856 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001857 scissorState.rect().fLeft,
1858 scissorState.rect().fTop,
1859 scissorState.rect().width(),
1860 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001861 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001862 // if the scissor fully contains the viewport then we fall through and
1863 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001864 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001865 if (fHWScissorSettings.fRect != scissor) {
1866 scissor.pushToGLScissor(this->glInterface());
1867 fHWScissorSettings.fRect = scissor;
1868 }
1869 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1870 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1871 fHWScissorSettings.fEnabled = kYes_TriState;
1872 }
1873 return;
1874 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001875 }
joshualitt77b13072014-10-27 14:51:01 -07001876
1877 // See fall through note above
1878 this->disableScissor();
1879}
1880
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001881void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
1882 const GrGLRenderTarget* rt) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001883#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001884 typedef GrWindowRectsState::Mode Mode;
1885 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1886 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001887
1888 if (!this->caps()->maxWindowRectangles() ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001889 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001890 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001891 }
1892
csmartdalton7535f412016-08-23 06:51:00 -07001893 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1894 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001895 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1896 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001897
csmartdalton28341fa2016-08-17 10:00:21 -07001898 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001899 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001900 for (int i = 0; i < numWindows; ++i) {
Brian Salomon9a767722017-03-13 17:57:28 -04001901 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07001902 }
1903
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001904 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001905 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001906
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001907 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001908#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001909}
1910
1911void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001912#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001913 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001914 return;
1915 }
1916 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001917 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001918#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001919}
1920
ethannicholas28ef4452016-03-25 09:26:03 -07001921void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1922 if (fHWMinSampleShading != minSampleShading) {
1923 if (minSampleShading > 0.0) {
1924 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1925 GL_CALL(MinSampleShading(minSampleShading));
1926 }
1927 else {
1928 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1929 }
1930 fHWMinSampleShading = minSampleShading;
1931 }
1932}
1933
bsalomon2eda5b32016-09-21 10:53:24 -07001934bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1935 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001936 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001937 if (!program) {
1938 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1939 return false;
1940 }
1941
1942 program->generateMipmaps(primProc, pipeline);
1943
egdaniel080e6732014-12-22 07:35:52 -08001944 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001945 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001946
egdaniel080e6732014-12-22 07:35:52 -08001947 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001948 this->flushDrawFace(pipeline.getDrawFace());
ethannicholas28ef4452016-03-25 09:26:03 -07001949 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001950
bsalomon6df86402015-06-01 10:41:49 -07001951 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001952 if (fHWProgramID != programID) {
1953 GL_CALL(UseProgram(programID));
1954 fHWProgramID = programID;
1955 }
1956
egdanield803f272015-03-18 13:01:52 -07001957 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001958 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001959 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07001960 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001961 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001962 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001963
cdalton74b8d322016-04-11 14:47:28 -07001964 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001965
brianosman898235c2016-04-06 07:38:23 -07001966 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001967 GrStencilSettings stencil;
1968 if (pipeline.isStencilEnabled()) {
1969 // TODO: attach stencil and create settings during render target flush.
1970 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1971 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1972 glRT->renderTargetPriv().numStencilBits());
1973 }
1974 this->flushStencil(stencil);
egdaniel8dd688b2015-01-22 10:16:09 -08001975 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001976 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
csmartdaltondabc91b2016-11-15 14:26:27 -07001977 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001978
1979 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001980 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001981 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001982
1983 return true;
1984}
1985
joshualitt873ad0e2015-01-20 09:08:51 -08001986void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001987 const GrBuffer* indexBuffer,
1988 const GrBuffer* vertexBuffer,
1989 int baseVertex) {
cdaltone2e71c22016-04-07 18:13:29 -07001990 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001991 if (indexBuffer) {
1992 SkASSERT(indexBuffer);
1993 SkASSERT(!indexBuffer->isMapped());
1994 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001995 } else {
1996 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001997 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001998
Chris Daltonff926502017-05-03 14:36:54 -04001999 SkASSERT(vertexBuffer);
2000 SkASSERT(!vertexBuffer->isMapped());
2001
joshualitt873ad0e2015-01-20 09:08:51 -08002002 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08002003 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002004
joshualitt873ad0e2015-01-20 09:08:51 -08002005 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08002006
Chris Daltonff926502017-05-03 14:36:54 -04002007 size_t vertexOffsetInBytes = stride * baseVertex;
bsalomonbc3d0de2014-12-15 13:45:03 -08002008
Chris Daltonff926502017-05-03 14:36:54 -04002009 vertexOffsetInBytes += vertexBuffer->baseOffset();
bsalomonbc3d0de2014-12-15 13:45:03 -08002010
bsalomonbc3d0de2014-12-15 13:45:03 -08002011 uint32_t usedAttribArraysMask = 0;
2012 size_t offset = 0;
2013
2014 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08002015 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08002016 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08002017 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08002018 attribState->set(this,
2019 attribIndex,
Chris Daltonff926502017-05-03 14:36:54 -04002020 vertexBuffer,
cdalton793dc262016-02-08 10:11:47 -08002021 attribType,
bsalomonbc3d0de2014-12-15 13:45:03 -08002022 stride,
2023 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08002024 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002025 }
2026 attribState->disableUnusedArrays(this, usedAttribArraysMask);
2027 }
2028}
2029
csmartdalton485a1202016-07-13 10:16:32 -07002030GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002031 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002032
cdaltone2e71c22016-04-07 18:13:29 -07002033 // Index buffer state is tied to the vertex array.
2034 if (kIndex_GrBufferType == type) {
2035 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002036 }
cdaltone2e71c22016-04-07 18:13:29 -07002037
2038 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2039 auto& bufferState = fHWBufferState[type];
2040
robertphillips8abb3702016-08-31 14:04:06 -07002041 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07002042 if (buffer->isCPUBacked()) {
2043 if (!bufferState.fBufferZeroKnownBound) {
2044 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
2045 }
2046 } else {
2047 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2048 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07002049 }
csmartdalton485a1202016-07-13 10:16:32 -07002050 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07002051 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002052 }
2053
2054 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002055}
2056
cdalton74b8d322016-04-11 14:47:28 -07002057void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
2058 if (buffer->hasAttachedToTexture()) {
2059 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05002060 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07002061 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
2062 auto& buffTex = fHWBufferTextures[i];
2063 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
2064 continue;
2065 }
2066 if (i == fHWMaxUsedBufferTextureUnit) {
2067 --fHWMaxUsedBufferTextureUnit;
2068 }
2069
2070 this->setTextureUnit(i);
2071 if (!buffTex.fKnownBound) {
2072 SkASSERT(buffTex.fTextureID);
2073 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
2074 buffTex.fKnownBound = true;
2075 }
2076 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
2077 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
2078 }
2079 }
2080}
2081
bsalomon861e1032014-12-16 07:33:49 -08002082void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002083 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002084 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002085 fHWScissorSettings.fEnabled = kNo_TriState;
2086 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002087 }
2088}
2089
csmartdalton29df7602016-08-31 11:55:52 -07002090void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07002091 this->handleDirtyContext();
2092
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002093 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002094 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002095 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002096
csmartdalton29df7602016-08-31 11:55:52 -07002097 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
2098 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002099 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00002100
2101 GrGLfloat r, g, b, a;
2102 static const GrGLfloat scale255 = 1.f / 255.f;
2103 a = GrColorUnpackA(color) * scale255;
2104 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002105 r = GrColorUnpackR(color) * scaleRGB;
2106 g = GrColorUnpackG(color) * scaleRGB;
2107 b = GrColorUnpackB(color) * scaleRGB;
2108
2109 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002110 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002111 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002112 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002113}
2114
bsalomon861e1032014-12-16 07:33:49 -08002115void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002116 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002117 return;
2118 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002119 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002120 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002121
joshualitt77b13072014-10-27 14:51:01 -07002122 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002123 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002124
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002125 GL_CALL(StencilMask(0xffffffff));
2126 GL_CALL(ClearStencil(0));
2127 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002128 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002129}
2130
csmartdalton29df7602016-08-31 11:55:52 -07002131void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2132 bool insideStencilMask,
2133 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07002134 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002135 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002136
egdaniel8dc7c3a2015-04-16 11:22:42 -07002137 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002138 // this should only be called internally when we know we have a
2139 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002140 SkASSERT(sb);
2141 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002142#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002143 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002144 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002145#else
2146 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002147 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002148 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002149 // changing the clip between stencil passes may or may not
2150 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002151 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002152#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002153 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002154 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002155 value = (1 << (stencilBitCount - 1));
2156 } else {
2157 value = 0;
2158 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002159 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002160 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002161
csmartdalton29df7602016-08-31 11:55:52 -07002162 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002163 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002164
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002165 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002166 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002167 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002168 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002169}
2170
Brian Salomon71d9d842016-11-03 13:42:00 -04002171static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002172 int width, int height, GrPixelConfig config,
2173 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002174 // If the surface is already TopLeft, we don't need to flip.
2175 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002176 return false;
2177 }
2178
bsalomon494aa592015-07-23 11:45:02 -07002179 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002180 static const int kMinSize = 32;
2181 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002182 return false;
2183 }
2184
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002185 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002186 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002187 return false;
2188 }
2189
2190 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002191 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002192 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2193 // clipped.
2194 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2195}
2196
bsalomon1aa20292016-01-22 08:16:09 -08002197bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002198#ifdef SK_BUILD_FOR_MAC
2199 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2200 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2201 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2202 // we always use a temporary surface to test for read pixels support.
2203 // https://www.crbug.com/662802
2204 if (this->glContext().driver() == kChromium_GrGLDriver) {
2205 return this->readPixelsSupported(target->config(), readConfig);
2206 }
2207#endif
bsalomon1aa20292016-01-22 08:16:09 -08002208 auto bindRenderTarget = [this, target]() -> bool {
2209 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2210 return true;
2211 };
bsalomon2c3db322016-11-08 13:26:24 -08002212 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002213 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2214 GR_GL_GetIntegerv(this->glInterface(), query, value);
2215 };
2216 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002217 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2218 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002219}
2220
2221bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002222 sk_sp<GrTexture> temp;
2223 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
bsalomon1aa20292016-01-22 08:16:09 -08002224 GrTextureDesc desc;
2225 desc.fConfig = rtConfig;
2226 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002227 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2228 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2229 temp.reset(this->createTexture(desc, SkBudgeted::kNo));
2230 if (!temp) {
2231 return false;
2232 }
2233 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2234 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2235 return true;
2236 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
2237 temp.reset(this->createTexture(desc, SkBudgeted::kNo));
2238 if (!temp) {
2239 return false;
2240 }
2241 GrGLIRect vp;
2242 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002243 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002244 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002245 }
bsalomon2c3db322016-11-08 13:26:24 -08002246 return false;
2247 };
2248 auto unbindRenderTarget = [this, &temp]() {
2249 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002250 };
2251 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2252 GR_GL_GetIntegerv(this->glInterface(), query, value);
2253 };
bsalomon2c3db322016-11-08 13:26:24 -08002254 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2255 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002256}
2257
2258bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2259 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2260 return this->readPixelsSupported(rt, readConfig);
2261 } else {
2262 GrPixelConfig config = surfaceForConfig->config();
2263 return this->readPixelsSupported(config, readConfig);
2264 }
2265}
2266
bsalomone9573312016-01-25 14:33:25 -08002267static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2268 if (GrPixelConfigIsSRGB(a)) {
2269 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2270 } else if (GrPixelConfigIsSRGB(b)) {
2271 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2272 }
2273 return false;
2274}
2275
bsalomonf0674512015-07-28 13:26:15 -07002276bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2277 GrPixelConfig readConfig, DrawPreference* drawPreference,
2278 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002279 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002280
bsalomone9573312016-01-25 14:33:25 -08002281 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002282 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2283 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2284 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2285 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2286 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002287 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2288 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002289 // For now assume no swizzling, we may change that below.
2290 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2291
2292 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2293 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002294 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002295 // base class will fail for us.
2296 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2297 tempDrawInfo->fReadConfig = readConfig;
2298
2299 if (requires_srgb_conversion(srcConfig, readConfig)) {
2300 if (!this->readPixelsSupported(readConfig, readConfig)) {
2301 return false;
2302 }
2303 // Draw to do srgb to linear conversion or vice versa.
2304 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2305 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2306 tempDrawInfo->fReadConfig = readConfig;
2307 return true;
2308 }
2309
bsalomon1aa20292016-01-22 08:16:09 -08002310 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2311 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002312 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002313 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2314 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002315 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002316 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002317 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002318 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2319 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002320 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002321 // Better to do a draw with a R/B swap and then read as the original config.
2322 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002323 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2324 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002325 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002326 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2327 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002328 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002329 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002330 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2331 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2332 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002333 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2334 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2335 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2336 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002337 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002338 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002339 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2340 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2341 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2342 // will effectively be sBGRA).
2343 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2344 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2345 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2346 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002347 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2348 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2349 // it's unsupported, but 32bit RGBA reads are supported.
2350 // Don't attempt to do any srgb conversions since we only care about alpha.
2351 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2352 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2353 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2354 }
2355 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2356 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2357 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002358 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002359 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2360 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2361 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2362 } else {
2363 return false;
2364 }
2365 } else {
2366 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2367 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2368 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002369 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002370 this->readPixelsSupported(readConfig, readConfig)) {
2371 // Do a draw to convert from the src config to the read config.
2372 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2373 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2374 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002375 } else {
2376 return false;
2377 }
bsalomon39826022015-07-23 08:07:21 -07002378 }
2379
Brian Salomon71d9d842016-11-03 13:42:00 -04002380 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
2381 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
2382 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002383 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002384 }
2385
bsalomon39826022015-07-23 08:07:21 -07002386 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002387}
2388
bsalomon6cb3cbe2015-07-30 07:34:27 -07002389bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002390 int left, int top,
2391 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002392 GrPixelConfig config,
2393 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002394 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002395 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002396
bsalomone9573312016-01-25 14:33:25 -08002397 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002398 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002399 return false;
2400 }
2401
bsalomon16921ec2015-07-30 15:34:56 -07002402 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002403 if (requires_srgb_conversion(surface->config(), config)) {
2404 return false;
2405 }
2406
2407 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2408 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002409 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002410 // Don't attempt to do any srgb conversions since we only care about alpha.
2411 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002412 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002413 tempConfig = kSRGBA_8888_GrPixelConfig;
2414 }
2415 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002416 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002417 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Brian Salomon71d9d842016-11-03 13:42:00 -04002418 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
bsalomone9573312016-01-25 14:33:25 -08002419 width*4)) {
2420 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2421 for (int j = 0; j < height; ++j) {
2422 for (int i = 0; i < width; ++i) {
2423 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2424 }
2425 }
2426 return true;
2427 }
2428 }
bsalomon16921ec2015-07-30 15:34:56 -07002429 return false;
2430 }
2431
bsalomon76148af2016-01-12 11:13:47 -08002432 GrGLenum externalFormat;
2433 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002434 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002435 &externalType)) {
2436 return false;
2437 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002438 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002439
Brian Salomon71d9d842016-11-03 13:42:00 -04002440 GrGLIRect glvp;
2441 if (renderTarget) {
2442 // resolve the render target if necessary
2443 switch (renderTarget->getResolveType()) {
2444 case GrGLRenderTarget::kCantResolve_ResolveType:
2445 return false;
2446 case GrGLRenderTarget::kAutoResolves_ResolveType:
2447 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2448 break;
2449 case GrGLRenderTarget::kCanResolve_ResolveType:
2450 this->onResolveRenderTarget(renderTarget);
2451 // we don't track the state of the READ FBO ID.
2452 fStats.incRenderTargetBinds();
2453 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2454 break;
2455 default:
2456 SkFAIL("Unknown resolve type");
2457 }
2458 glvp = renderTarget->getViewport();
2459 } else {
2460 // Use a temporary FBO.
2461 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002462 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002463 }
2464
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002465 // the read rect is viewport-relative
2466 GrGLIRect readRect;
Brian Salomon71d9d842016-11-03 13:42:00 -04002467 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002468
bsalomon9d02b262016-02-01 12:49:30 -08002469 size_t bytesPerPixel = GrBytesPerPixel(config);
2470 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002471
bsalomon@google.comc6980972011-11-02 19:57:21 +00002472 size_t readDstRowBytes = tightRowBytes;
2473 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002474
bsalomon@google.comc6980972011-11-02 19:57:21 +00002475 // determine if GL can read using the passed rowBytes or if we need
2476 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002477 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002478 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002479 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002480 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002481 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002482 readDstRowBytes = rowBytes;
2483 } else {
2484 scratch.reset(tightRowBytes * height);
2485 readDst = scratch.get();
2486 }
2487 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002488 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002489 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2490 }
bsalomonf46a1242015-12-15 12:37:38 -08002491 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2492
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002493 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2494 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002495 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002496 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002497 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002498 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2499 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002500 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002501 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002502 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002503 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002504
2505 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002506 // API presents top-to-bottom. We must preserve the padding contents. Note
2507 // that the above readPixels did not overwrite the padding.
2508 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002509 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002510 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002511 scratch.reset(tightRowBytes);
2512 void* tmpRow = scratch.get();
2513 // flip y in-place by rows
2514 const int halfY = height >> 1;
2515 char* top = reinterpret_cast<char*>(buffer);
2516 char* bottom = top + (height - 1) * rowBytes;
2517 for (int y = 0; y < halfY; y++) {
2518 memcpy(tmpRow, top, tightRowBytes);
2519 memcpy(top, bottom, tightRowBytes);
2520 memcpy(bottom, tmpRow, tightRowBytes);
2521 top += rowBytes;
2522 bottom -= rowBytes;
2523 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002524 }
2525 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002526 SkASSERT(readDst != buffer);
2527 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002528 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002529 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002530 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002531 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002532 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002533 dst += (height-1) * rowBytes;
2534 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002535 for (int y = 0; y < height; y++) {
2536 memcpy(dst, src, tightRowBytes);
2537 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002538 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002539 dst += rowBytes;
2540 } else {
2541 dst -= rowBytes;
2542 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002543 }
2544 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002545 if (!renderTarget) {
2546 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2547 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002548 return true;
2549}
2550
egdaniel9cb63402016-06-23 08:37:05 -07002551GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -07002552 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2553 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -05002554 return new GrGLGpuCommandBuffer(this);
egdaniel066df7c2016-06-08 14:02:27 -07002555}
2556
brianosman64d094d2016-03-25 06:01:59 -07002557void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002558 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002559
Robert Phillips294870f2016-11-11 12:38:40 -05002560 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002561 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002562 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002563 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2564#ifdef SK_DEBUG
2565 // don't do this check in Chromium -- this is causing
2566 // lots of repeated command buffer flushes when the compositor is
2567 // rendering with Ganesh, which is really slow; even too slow for
2568 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002569 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002570 GrGLenum status;
2571 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2572 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2573 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2574 }
bsalomon160f24c2015-03-17 15:55:42 -07002575 }
egdanield803f272015-03-18 13:01:52 -07002576#endif
2577 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002578 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002579 }
2580
brianosman35b784d2016-05-05 11:52:53 -07002581 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002582 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002583 }
brianosman64d094d2016-03-25 06:01:59 -07002584
bsalomon083617b2016-02-12 12:10:14 -08002585 this->didWriteToSurface(target, bounds);
2586}
bsalomona9909122016-01-23 10:41:40 -08002587
brianosman33f6b3f2016-06-02 05:49:21 -07002588void GrGLGpu::flushFramebufferSRGB(bool enable) {
2589 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2590 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2591 fHWSRGBFramebuffer = kYes_TriState;
2592 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2593 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2594 fHWSRGBFramebuffer = kNo_TriState;
2595 }
2596}
2597
bsalomon083617b2016-02-12 12:10:14 -08002598void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2599 if (fHWViewport != viewport) {
2600 viewport.pushToGLViewport(this->glInterface());
2601 fHWViewport = viewport;
2602 }
2603}
2604
twiz@google.com0f31ca72011-03-18 17:38:11 +00002605GrGLenum gPrimitiveType2GLMode[] = {
2606 GR_GL_TRIANGLES,
2607 GR_GL_TRIANGLE_STRIP,
2608 GR_GL_TRIANGLE_FAN,
2609 GR_GL_POINTS,
2610 GR_GL_LINES,
2611 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002612};
2613
bsalomon@google.comd302f142011-03-03 13:54:13 +00002614#define SWAP_PER_DRAW 0
2615
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002616#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002617 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002618 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002619 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002620 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002621 void SwapBuf() {
2622 DWORD procID = GetCurrentProcessId();
2623 HWND hwnd = GetTopWindow(GetDesktopWindow());
2624 while(hwnd) {
2625 DWORD wndProcID = 0;
2626 GetWindowThreadProcessId(hwnd, &wndProcID);
2627 if(wndProcID == procID) {
2628 SwapBuffers(GetDC(hwnd));
2629 }
2630 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2631 }
2632 }
2633 #endif
2634#endif
2635
egdaniel9cb63402016-06-23 08:37:05 -07002636void GrGLGpu::draw(const GrPipeline& pipeline,
2637 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002638 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002639 int meshCount) {
2640 this->handleDirtyContext();
2641
bsalomon2eda5b32016-09-21 10:53:24 -07002642 bool hasPoints = false;
2643 for (int i = 0; i < meshCount; ++i) {
Chris Daltonff926502017-05-03 14:36:54 -04002644 if (meshes[i].fPrimitiveType == kPoints_GrPrimitiveType) {
bsalomon2eda5b32016-09-21 10:53:24 -07002645 hasPoints = true;
2646 break;
2647 }
2648 }
2649 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002650 return;
2651 }
ethannicholas22793252016-01-30 09:59:10 -08002652
egdaniel0e1853c2016-03-17 11:35:45 -07002653 for (int i = 0; i < meshCount; ++i) {
2654 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2655 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2656 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002657
egdaniel0e1853c2016-03-17 11:35:45 -07002658 const GrMesh& mesh = meshes[i];
Chris Daltonff926502017-05-03 14:36:54 -04002659 for (GrMesh::PatternBatch batch : mesh) {
2660 this->setupGeometry(primProc, mesh.fIndexBuffer.get(), mesh.fVertexBuffer.get(),
2661 batch.fBaseVertex);
2662 if (const GrBuffer* indexBuffer = mesh.fIndexBuffer.get()) {
2663 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2664 sizeof(uint16_t) * mesh.fBaseIndex);
2665 // mesh.fBaseVertex was accounted for by setupGeometry.
bsalomonfc9527a2016-08-29 09:18:39 -07002666 if (this->glCaps().drawRangeElementsSupport()) {
Brian Salomon09d994e2016-12-21 11:14:46 -05002667 // We assume here that the GrMeshDrawOps that generated the mesh used the full
bsalomonfc9527a2016-08-29 09:18:39 -07002668 // 0..vertexCount()-1 range.
2669 int start = 0;
Chris Daltonff926502017-05-03 14:36:54 -04002670 int end = mesh.fVertexCount * batch.fRepeatCount - 1;
2671 GL_CALL(DrawRangeElements(gPrimitiveType2GLMode[mesh.fPrimitiveType],
bsalomonfc9527a2016-08-29 09:18:39 -07002672 start, end,
Chris Daltonff926502017-05-03 14:36:54 -04002673 mesh.fIndexCount * batch.fRepeatCount,
bsalomonfc9527a2016-08-29 09:18:39 -07002674 GR_GL_UNSIGNED_SHORT,
2675 indices));
2676 } else {
Chris Daltonff926502017-05-03 14:36:54 -04002677 GL_CALL(DrawElements(gPrimitiveType2GLMode[mesh.fPrimitiveType],
2678 mesh.fIndexCount * batch.fRepeatCount,
bsalomonfc9527a2016-08-29 09:18:39 -07002679 GR_GL_UNSIGNED_SHORT,
2680 indices));
2681 }
egdaniel0e1853c2016-03-17 11:35:45 -07002682 } else {
2683 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account
Chris Daltonff926502017-05-03 14:36:54 -04002684 // for mesh.fBaseVertex in the DrawElements case. So we always rely on setupGeometry
2685 // to have accounted for mesh.fBaseVertex.
2686 GL_CALL(DrawArrays(gPrimitiveType2GLMode[mesh.fPrimitiveType], 0,
2687 mesh.fVertexCount * batch.fRepeatCount));
egdaniel0e1853c2016-03-17 11:35:45 -07002688 }
2689 fStats.incNumDraws();
Chris Daltonff926502017-05-03 14:36:54 -04002690 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002691 }
ethannicholas22793252016-01-30 09:59:10 -08002692
bsalomon@google.comd302f142011-03-03 13:54:13 +00002693#if SWAP_PER_DRAW
2694 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002695 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002696 aglSwapBuffers(aglGetCurrentContext());
2697 int set_a_break_pt_here = 9;
2698 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002699 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002700 SwapBuf();
2701 int set_a_break_pt_here = 9;
2702 SwapBuf();
2703 #endif
2704#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002705}
2706
bsalomon861e1032014-12-16 07:33:49 -08002707void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002708 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002709 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002710 // Some extensions automatically resolves the texture when it is read.
2711 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002712 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2713 fStats.incRenderTargetBinds();
2714 fStats.incRenderTargetBinds();
2715 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2716 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2717 // make sure we go through flushRenderTarget() since we've modified
2718 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002719 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002720 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002721 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002722
bsalomon@google.com347c3822013-05-01 20:10:01 +00002723 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002724 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002725 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002726 scissorState.set(dirtyRect);
2727 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002728 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002729 GL_CALL(ResolveMultisampleFramebuffer());
2730 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002731 int l, b, r, t;
2732 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2733 this->glCaps().blitFramebufferSupportFlags()) {
2734 l = 0;
2735 b = 0;
2736 r = target->width();
2737 t = target->height();
2738 } else {
2739 GrGLIRect rect;
2740 rect.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2741 dirtyRect.width(), dirtyRect.height(), target->origin());
2742 l = rect.fLeft;
2743 b = rect.fBottom;
2744 r = rect.fLeft + rect.fWidth;
2745 t = rect.fBottom + rect.fHeight;
2746 }
derekf8c8f71a2014-09-16 06:24:57 -07002747
2748 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002749 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002750 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002751 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002752 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002753 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002754 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002755 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002756 }
2757}
2758
bsalomon@google.com411dad02012-06-05 20:24:20 +00002759namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002760
bsalomon@google.com411dad02012-06-05 20:24:20 +00002761
2762GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002763 static const GrGLenum gTable[kGrStencilOpCount] = {
2764 GR_GL_KEEP, // kKeep
2765 GR_GL_ZERO, // kZero
2766 GR_GL_REPLACE, // kReplace
2767 GR_GL_INVERT, // kInvert
2768 GR_GL_INCR_WRAP, // kIncWrap
2769 GR_GL_DECR_WRAP, // kDecWrap
2770 GR_GL_INCR, // kIncClamp
2771 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002772 };
cdalton93a379b2016-05-11 13:58:08 -07002773 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2774 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2775 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2776 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2777 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2778 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2779 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2780 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2781 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2782 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002783}
2784
2785void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002786 const GrStencilSettings::Face& face,
2787 GrGLenum glFace) {
2788 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2789 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2790 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002791
cdalton93a379b2016-05-11 13:58:08 -07002792 GrGLint ref = face.fRef;
2793 GrGLint mask = face.fTestMask;
2794 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002795
2796 if (GR_GL_FRONT_AND_BACK == glFace) {
2797 // we call the combined func just in case separate stencil is not
2798 // supported.
2799 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2800 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002801 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002802 } else {
2803 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2804 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002805 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002806 }
2807}
2808}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002809
bsalomon3e791242014-12-17 13:43:13 -08002810void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002811 if (stencilSettings.isDisabled()) {
2812 this->disableStencil();
2813 } else if (fHWStencilSettings != stencilSettings) {
2814 if (kYes_TriState != fHWStencilTestEnabled) {
2815 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2816 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002817 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002818 if (stencilSettings.isTwoSided()) {
2819 SkASSERT(this->caps()->twoSidedStencilSupport());
2820 set_gl_stencil(this->glInterface(),
2821 stencilSettings.front(),
2822 GR_GL_FRONT);
2823 set_gl_stencil(this->glInterface(),
2824 stencilSettings.back(),
2825 GR_GL_BACK);
2826 } else {
2827 set_gl_stencil(this->glInterface(),
2828 stencilSettings.front(),
2829 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002830 }
joshualitta58fe352014-10-27 08:39:00 -07002831 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002832 }
2833}
2834
csmartdaltonc7d85332016-10-26 10:13:46 -07002835void GrGLGpu::disableStencil() {
2836 if (kNo_TriState != fHWStencilTestEnabled) {
2837 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2838 fHWStencilTestEnabled = kNo_TriState;
2839 fHWStencilSettings.invalidate();
2840 }
2841}
2842
cdaltonaf8bc7d2016-02-05 09:35:20 -08002843void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002844 // rt is only optional if useHWAA is false.
2845 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002846 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002847
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002848 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002849 if (useHWAA) {
2850 if (kYes_TriState != fMSAAEnabled) {
2851 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2852 fMSAAEnabled = kYes_TriState;
2853 }
2854 } else {
2855 if (kNo_TriState != fMSAAEnabled) {
2856 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2857 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002858 }
2859 }
2860 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002861
2862 if (0 != this->caps()->maxRasterSamples()) {
csmartdaltonf9635992016-08-10 11:09:07 -07002863 if (useHWAA && rt->isMixedSampled() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002864 // Since stencil is disabled and we want more samples than are in the color buffer, we
2865 // need to tell the rasterizer explicitly how many to run.
2866 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2867 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2868 fHWRasterMultisampleEnabled = kYes_TriState;
2869 }
2870 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2871 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2872 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2873 fHWNumRasterSamples = rt->numStencilSamples();
2874 }
2875 } else {
2876 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2877 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2878 fHWRasterMultisampleEnabled = kNo_TriState;
2879 }
2880 }
2881 } else {
csmartdaltonf9635992016-08-10 11:09:07 -07002882 SkASSERT(!useHWAA || !rt->isMixedSampled() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002883 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002884}
2885
bsalomon7f9b2e42016-01-12 13:29:26 -08002886void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002887 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002888 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002889
cdalton8917d622015-05-06 13:40:21 -07002890 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002891 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2892 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002893 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2894 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002895 if (blendOff) {
2896 if (kNo_TriState != fHWBlendState.fEnabled) {
2897 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002898
2899 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2900 // https://code.google.com/p/skia/issues/detail?id=3943
2901 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2902 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2903 SkASSERT(this->caps()->advancedBlendEquationSupport());
2904 // Set to any basic blending equation.
2905 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2906 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2907 fHWBlendState.fEquation = blend_equation;
2908 }
2909
egdanielb414f252014-07-29 13:15:47 -07002910 fHWBlendState.fEnabled = kNo_TriState;
2911 }
cdalton8917d622015-05-06 13:40:21 -07002912 return;
2913 }
2914
2915 if (kYes_TriState != fHWBlendState.fEnabled) {
2916 GL_CALL(Enable(GR_GL_BLEND));
2917 fHWBlendState.fEnabled = kYes_TriState;
2918 }
2919
2920 if (fHWBlendState.fEquation != equation) {
2921 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2922 fHWBlendState.fEquation = equation;
2923 }
2924
2925 if (GrBlendEquationIsAdvanced(equation)) {
2926 SkASSERT(this->caps()->advancedBlendEquationSupport());
2927 // Advanced equations have no other blend state.
2928 return;
2929 }
2930
bsalomone63ffef2016-02-05 07:17:34 -08002931 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002932 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2933 gXfermodeCoeff2Blend[dstCoeff]));
2934 fHWBlendState.fSrcCoeff = srcCoeff;
2935 fHWBlendState.fDstCoeff = dstCoeff;
2936 }
2937
bsalomon7f9b2e42016-01-12 13:29:26 -08002938 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2939 GrColor blendConst = blendInfo.fBlendConstant;
2940 blendConst = swizzle.applyTo(blendConst);
2941 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2942 GrGLfloat c[4];
2943 GrColorToRGBAFloat(blendConst, c);
2944 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2945 fHWBlendState.fConstColor = blendConst;
2946 fHWBlendState.fConstColorValid = true;
2947 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002948 }
2949}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002950
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002951static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002952 static const GrGLenum gWrapModes[] = {
2953 GR_GL_CLAMP_TO_EDGE,
2954 GR_GL_REPEAT,
2955 GR_GL_MIRRORED_REPEAT
2956 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002957 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002958 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2959 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2960 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2961 return gWrapModes[tm];
2962}
2963
egdanielb7e7d572015-11-04 04:23:53 -08002964static GrGLenum get_component_enum_from_char(char component) {
2965 switch (component) {
2966 case 'r':
2967 return GR_GL_RED;
2968 case 'g':
2969 return GR_GL_GREEN;
2970 case 'b':
2971 return GR_GL_BLUE;
2972 case 'a':
2973 return GR_GL_ALPHA;
2974 default:
2975 SkFAIL("Unsupported component");
2976 return 0;
2977 }
2978}
2979
2980/** If texture swizzling is available using tex parameters then it is preferred over mangling
2981 the generated shader code. This potentially allows greater reuse of cached shaders. */
2982static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002983 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002984 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002985 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002986 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002987 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002988 }
2989}
2990
Brian Salomon514baff2016-11-17 15:17:07 -05002991void GrGLGpu::bindTexture(int unitIdx, const GrSamplerParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -07002992 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002993 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002994
reed856e9d92015-09-30 12:21:45 -07002995#ifdef SK_DEBUG
2996 if (!this->caps()->npotTextureTileSupport()) {
2997 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2998 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2999 if (tileX || tileY) {
3000 const int w = texture->width();
3001 const int h = texture->height();
3002 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3003 }
3004 }
3005#endif
3006
bsalomon@google.comb8670992012-07-25 21:27:09 +00003007 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3008 // 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 +00003009 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003010 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003011 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003012 this->onResolveRenderTarget(texRT);
3013 }
3014
Robert Phillips294870f2016-11-11 12:38:40 -05003015 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003016 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003017 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003018 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003019 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003020 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003021 }
3022
bsalomon@google.com4c883782012-06-04 19:05:11 +00003023 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003024 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003025 bool setAll = timestamp < this->getResetTimestamp();
3026 GrGLTexture::TexParams newTexParams;
3027
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003028 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003029 GR_GL_NEAREST,
3030 GR_GL_LINEAR,
3031 GR_GL_LINEAR_MIPMAP_LINEAR
3032 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003033 static GrGLenum glMagFilterModes[] = {
3034 GR_GL_NEAREST,
3035 GR_GL_LINEAR,
3036 GR_GL_LINEAR
3037 };
Brian Salomon514baff2016-11-17 15:17:07 -05003038 GrSamplerParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003039
Brian Salomon514baff2016-11-17 15:17:07 -05003040 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
bsalomonefd7d452014-10-23 14:17:46 -07003041 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
Brian Salomon514baff2016-11-17 15:17:07 -05003042 filterMode = GrSamplerParams::kBilerp_FilterMode;
bsalomonefd7d452014-10-23 14:17:46 -07003043 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003044 }
bsalomonefd7d452014-10-23 14:17:46 -07003045
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003046 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3047 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003048
brianosman20471892016-12-02 06:43:32 -08003049 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003050 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003051 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003052 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003053 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003054 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003055 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003056
brianosman33f6b3f2016-06-02 05:49:21 -07003057#ifdef SK_DEBUG
3058 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon514baff2016-11-17 15:17:07 -05003059 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003060 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3061 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003062 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3063 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3064 : SkDestinationSurfaceColorMode::kLegacy;
3065 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003066 }
3067 }
3068#endif
3069
cblume55f2d2d2016-02-26 13:20:48 -08003070 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3071
bsalomon@google.comb8670992012-07-25 21:27:09 +00003072 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3073 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003074 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003075 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003076 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003077 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003078 }
3079 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3080 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003081 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003082 }
cblume55f2d2d2016-02-26 13:20:48 -08003083 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003084 // These are not supported in ES2 contexts
3085 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3086 if (newTexParams.fMaxMipMapLevel != 0) {
3087 this->setTextureUnit(unitIdx);
3088 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3089 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3090 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3091 newTexParams.fMaxMipMapLevel));
3092 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3093 newTexParams.fMaxMipMapLevel));
3094 }
cblume55f2d2d2016-02-26 13:20:48 -08003095 }
3096 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003097 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003098 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003099 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003100 }
3101 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003102 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003103 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003104 }
bsalomoncdee0092016-01-08 13:20:12 -08003105 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003106 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3107 oldTexParams.fSwizzleRGBA,
3108 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003109 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003110 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003111 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003112}
3113
csmartdalton1897cfd2016-06-03 08:50:54 -07003114void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003115 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3116 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003117
3118 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3119
3120 if (!buffTex.fKnownBound) {
3121 if (!buffTex.fTextureID) {
3122 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3123 if (!buffTex.fTextureID) {
3124 return;
3125 }
3126 }
3127
3128 this->setTextureUnit(unitIdx);
3129 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3130
3131 buffTex.fKnownBound = true;
3132 }
3133
robertphillips8abb3702016-08-31 14:04:06 -07003134 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003135 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003136
3137 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003138 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3139 this->glCaps().configSizedInternalFormat(texelConfig),
3140 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003141
cdalton74b8d322016-04-11 14:47:28 -07003142 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003143 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003144
3145 if (this->glCaps().textureSwizzleSupport() &&
3146 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3147 GrGLenum glSwizzle[4];
3148 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3149 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3150 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3151 }
3152
3153 buffer->setHasAttachedToTexture();
3154 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3155 }
3156}
3157
Brian Salomonf9f45122016-11-29 11:59:17 -05003158void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3159 SkASSERT(texture);
3160 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3161 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003162 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003163 switch (ioType) {
3164 case kRead_GrIOType:
3165 access = GR_GL_READ_ONLY;
3166 break;
3167 case kWrite_GrIOType:
3168 access = GR_GL_WRITE_ONLY;
3169 break;
3170 case kRW_GrIOType:
3171 access = GR_GL_READ_WRITE;
3172 break;
3173 }
3174 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3175 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3176 }
3177}
3178
Brian Salomon514baff2016-11-17 15:17:07 -05003179void GrGLGpu::generateMipmaps(const GrSamplerParams& params, bool allowSRGBInputs,
brianosman33f6b3f2016-06-02 05:49:21 -07003180 GrGLTexture* texture) {
3181 SkASSERT(texture);
3182
3183 // First, figure out if we need mips for this texture at all:
Brian Salomon514baff2016-11-17 15:17:07 -05003184 GrSamplerParams::FilterMode filterMode = params.filterMode();
brianosman33f6b3f2016-06-02 05:49:21 -07003185
Brian Salomon514baff2016-11-17 15:17:07 -05003186 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003187 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
Brian Salomon514baff2016-11-17 15:17:07 -05003188 filterMode = GrSamplerParams::kBilerp_FilterMode;
brianosman33f6b3f2016-06-02 05:49:21 -07003189 }
3190 }
3191
Brian Salomon514baff2016-11-17 15:17:07 -05003192 if (GrSamplerParams::kMipMap_FilterMode != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003193 return;
3194 }
3195
3196 // If this is an sRGB texture and the mips were previously built the "other" way
3197 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3198 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003199 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3200 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3201 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003202 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003203 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003204 texture->texturePriv().dirtyMipMaps(true);
3205 }
3206
3207 // If the mips aren't dirty, we're done:
3208 if (!texture->texturePriv().mipMapsAreDirty()) {
3209 return;
3210 }
3211
3212 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3213 // from the rt it will still be the last bound texture, but it needs resolving.
3214 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3215 if (texRT) {
3216 this->onResolveRenderTarget(texRT);
3217 }
3218
3219 GrGLenum target = texture->target();
3220 this->setScratchTextureUnit();
3221 GL_CALL(BindTexture(target, texture->textureID()));
3222
3223 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3224 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003225 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003226 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3227 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3228 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3229 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3230 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3231 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3232 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3233 srgbDecode = GR_GL_DECODE_EXT;
3234 }
3235 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003236 }
3237
3238 // Either do manual mipmap generation or (if that fails), just rely on the driver:
3239 if (!this->generateMipmap(texture, allowSRGBInputs)) {
3240 GL_CALL(GenerateMipmap(target));
3241 }
3242
brianosmanfe199872016-06-13 07:59:48 -07003243 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003244 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3245 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003246 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003247
3248 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3249 texture->textureParamsModified();
3250}
3251
cdalton74b8d322016-04-11 14:47:28 -07003252void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3253 this->setTextureUnit(unitIdx);
3254 if (this->glStandard() == kGLES_GrGLStandard) {
3255 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3256 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3257 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3258 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3259 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3260 } else {
3261 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3262 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3263 reinterpret_cast<const GrGLint*>(swizzle)));
3264 }
3265}
3266
egdaniel080e6732014-12-22 07:35:52 -08003267void GrGLGpu::flushColorWrite(bool writeColor) {
3268 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003269 if (kNo_TriState != fHWWriteToColor) {
3270 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3271 GR_GL_FALSE, GR_GL_FALSE));
3272 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003273 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003274 } else {
3275 if (kYes_TriState != fHWWriteToColor) {
3276 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3277 fHWWriteToColor = kYes_TriState;
3278 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003279 }
bsalomon3e791242014-12-17 13:43:13 -08003280}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003281
robertphillips5fa7f302016-07-21 09:21:04 -07003282void GrGLGpu::flushDrawFace(GrDrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003283 if (fHWDrawFace != face) {
3284 switch (face) {
robertphillips5fa7f302016-07-21 09:21:04 -07003285 case GrDrawFace::kCCW:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003286 GL_CALL(Enable(GR_GL_CULL_FACE));
3287 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003288 break;
robertphillips5fa7f302016-07-21 09:21:04 -07003289 case GrDrawFace::kCW:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003290 GL_CALL(Enable(GR_GL_CULL_FACE));
3291 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003292 break;
robertphillips5fa7f302016-07-21 09:21:04 -07003293 case GrDrawFace::kBoth:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003294 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003295 break;
3296 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003297 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003298 }
bsalomon3e791242014-12-17 13:43:13 -08003299 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003300 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003301}
3302
bsalomon861e1032014-12-16 07:33:49 -08003303void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003304 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003305 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003306 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003307 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003308 }
3309}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003310
bsalomon861e1032014-12-16 07:33:49 -08003311void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003312 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003313 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003314 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3315 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3316 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003317 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003318 // clear out the this field so that if a program does use this unit it will rebind the correct
3319 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003320 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003321}
3322
Brian Salomone5e7eb12016-10-14 16:18:33 -04003323// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
3324static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3325 const GrSurface* src,
3326 const SkIRect& srcRect,
3327 const SkIPoint& dstPoint,
3328 const GrGLGpu* gpu) {
3329 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003330 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3331 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003332 return false;
3333 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003334 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3335 // have filtered such cases out.
3336 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003337 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3338 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3339 const GrRenderTarget* dstRT = dst->asRenderTarget();
3340 const GrRenderTarget* srcRT = src->asRenderTarget();
3341 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3342 return false;
3343 }
3344 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3345 return false;
3346 }
3347 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3348 return false;
3349 }
3350 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3351 // We would mirror to compensate for origin changes. Note that copySurface is
3352 // specified such that the src and dst rects are the same.
3353 if (dst->origin() != src->origin()) {
3354 return false;
3355 }
3356 }
3357 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003358 if (srcRT && srcRT->numColorSamples()) {
3359 if (dstRT && !dstRT->numColorSamples()) {
3360 return false;
3361 }
3362 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3363 return false;
3364 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003365 }
3366 }
3367 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3368 if (dstRT && dstRT->numColorSamples() > 0) {
3369 return false;
3370 }
3371 }
3372 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3373 if (dst->config() != src->config()) {
3374 return false;
3375 }
3376 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3377 const GrRenderTarget* srcRT = src->asRenderTarget();
3378 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3379 return false;
3380 }
3381 }
3382 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003383 if (srcRT && srcRT->numColorSamples()) {
3384 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3385 return false;
3386 }
3387 if (dst->origin() != src->origin()) {
3388 return false;
3389 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003390 }
3391 }
3392 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003393}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003394
bsalomon7ea33f52015-11-22 14:51:00 -08003395static inline bool can_copy_texsubimage(const GrSurface* dst,
3396 const GrSurface* src,
3397 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003398 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3399 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3400 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003401 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003402 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3403 return false;
3404 }
3405 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3406 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3407 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003408 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003409 return false;
3410 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003411 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3412 // If the src is multisampled (and uses an extension where there is a separate MSAA
3413 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003414 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003415 return false;
3416 }
bsalomon7ea33f52015-11-22 14:51:00 -08003417
3418 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3419 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3420 // texture.
3421 if (!dstTex) {
3422 return false;
3423 }
3424
3425 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003426
bsalomon7ea33f52015-11-22 14:51:00 -08003427 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3428 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003429 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003430 !GrPixelConfigIsCompressed(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003431 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
bsalomon7ea33f52015-11-22 14:51:00 -08003432 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003433 return true;
3434 } else {
3435 return false;
3436 }
3437}
3438
3439// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3440// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003441void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3442 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003443 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003444 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003445 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003446 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003447 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003448 GrGLuint* tempFBOID;
3449 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003450
egdanield803f272015-03-18 13:01:52 -07003451 if (0 == *tempFBOID) {
3452 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003453 }
3454
egdanield803f272015-03-18 13:01:52 -07003455 fStats.incRenderTargetBinds();
3456 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3457 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003458 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003459 target,
robertphillips754f4e92014-09-18 13:52:08 -07003460 texID,
3461 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003462 viewport->fLeft = 0;
3463 viewport->fBottom = 0;
3464 viewport->fWidth = surface->width();
3465 viewport->fHeight = surface->height();
3466 } else {
egdanield803f272015-03-18 13:01:52 -07003467 fStats.incRenderTargetBinds();
3468 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003469 *viewport = rt->getViewport();
3470 }
egdaniel0f5f9672015-02-03 11:10:51 -08003471}
3472
Brian Salomon71d9d842016-11-03 13:42:00 -04003473void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3474 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003475 if (!surface->asRenderTarget()) {
3476 SkASSERT(surface->asTexture());
3477 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3478 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3479 GR_GL_COLOR_ATTACHMENT0,
3480 textureTarget,
3481 0,
3482 0));
3483 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003484}
3485
joshualitt1cbdcde2015-08-21 11:53:29 -07003486bool GrGLGpu::onCopySurface(GrSurface* dst,
3487 GrSurface* src,
3488 const SkIRect& srcRect,
3489 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003490 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3491 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003492 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3493 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003494 return false;
3495 }
bsalomon083617b2016-02-12 12:10:14 -08003496 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3497 bool preferCopy = SkToBool(dst->asRenderTarget());
3498 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003499 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3500 return true;
3501 }
bsalomon5df6fee2015-05-18 06:26:15 -07003502 }
cblume61214052016-01-26 09:10:48 -08003503
bsalomon6df86402015-06-01 10:41:49 -07003504 if (can_copy_texsubimage(dst, src, this)) {
3505 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3506 return true;
3507 }
3508
Brian Salomone5e7eb12016-10-14 16:18:33 -04003509 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003510 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3511 }
3512
bsalomon083617b2016-02-12 12:10:14 -08003513 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003514 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3515 return true;
3516 }
bsalomon083617b2016-02-12 12:10:14 -08003517 }
3518
bsalomon6df86402015-06-01 10:41:49 -07003519 return false;
3520}
3521
Brian Salomonbf7b6202016-11-11 16:08:03 -05003522bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
3523 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003524 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003525 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003526
3527 if (!fCopyProgramArrayBuffer) {
3528 static const GrGLfloat vdata[] = {
3529 0, 0,
3530 0, 1,
3531 1, 0,
3532 1, 1
3533 };
3534 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3535 kStatic_GrAccessPattern, vdata));
3536 }
3537 if (!fCopyProgramArrayBuffer) {
3538 return false;
3539 }
3540
3541 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3542 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3543 if (!fCopyPrograms[progIdx].fProgram) {
3544 return false;
3545 }
3546
Brian Salomon1edc5b92016-11-29 13:43:46 -05003547 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003548 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3549 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3550 GrShaderVar::kUniform_TypeModifier);
3551 GrShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3552 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3553 GrShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier);
3554 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003555
3556 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003557 if (shaderCaps->noperspectiveInterpolationSupport()) {
3558 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003559 vshaderTxt.appendf("#extension %s : require\n", extension);
3560 }
3561 vTexCoord.addModifier("noperspective");
3562 }
3563
Brian Salomon1edc5b92016-11-29 13:43:46 -05003564 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003565 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003566 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003567 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003568 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003569 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003570 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003571 vshaderTxt.append(";");
3572
3573 vshaderTxt.append(
3574 "// Copy Program VS\n"
3575 "void main() {"
3576 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3577 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3578 " gl_Position.zw = vec2(0, 1);"
3579 "}"
3580 );
3581
3582 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003583 if (shaderCaps->noperspectiveInterpolationSupport()) {
3584 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003585 fshaderTxt.appendf("#extension %s : require\n", extension);
3586 }
3587 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003588 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003589 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003590 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003591 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003592 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
cdaltone2e71c22016-04-07 18:13:29 -07003593 &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -05003594 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003595 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003596 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003597 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003598 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003599 fshaderTxt.appendf(
3600 "// Copy Program FS\n"
3601 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003602 " sk_FragColor = texture(u_texture, v_texCoord);"
3603 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003604 );
3605
3606 const char* str;
3607 GrGLint length;
3608
3609 str = vshaderTxt.c_str();
3610 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003611 SkSL::Program::Settings settings;
3612 settings.fCaps = shaderCaps;
3613 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003614 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3615 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003616 &fStats, settings, &inputs);
3617 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003618
3619 str = fshaderTxt.c_str();
3620 length = SkToInt(fshaderTxt.size());
3621 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3622 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003623 &fStats, settings, &inputs);
3624 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003625
3626 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3627
3628 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3629 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3630 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3631 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3632 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3633 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3634
3635 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3636
3637 GL_CALL(DeleteShader(vshader));
3638 GL_CALL(DeleteShader(fshader));
3639
3640 return true;
bsalomon6df86402015-06-01 10:41:49 -07003641}
3642
brianosman33f6b3f2016-06-02 05:49:21 -07003643bool GrGLGpu::createMipmapProgram(int progIdx) {
3644 const bool oddWidth = SkToBool(progIdx & 0x2);
3645 const bool oddHeight = SkToBool(progIdx & 0x1);
3646 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3647
Brian Salomon1edc5b92016-11-29 13:43:46 -05003648 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003649
3650 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3651 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3652 if (!fMipmapPrograms[progIdx].fProgram) {
3653 return false;
3654 }
3655
Brian Salomon1edc5b92016-11-29 13:43:46 -05003656 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003657 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3658 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3659 GrShaderVar::kUniform_TypeModifier);
3660 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3661 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003662 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003663 GrShaderVar vTexCoords[] = {
3664 GrShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3665 GrShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3666 GrShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3667 GrShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003668 };
Brian Salomon99938a82016-11-21 13:41:08 -05003669 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003670
3671 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003672 if (shaderCaps->noperspectiveInterpolationSupport()) {
3673 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003674 vshaderTxt.appendf("#extension %s : require\n", extension);
3675 }
3676 vTexCoords[0].addModifier("noperspective");
3677 vTexCoords[1].addModifier("noperspective");
3678 vTexCoords[2].addModifier("noperspective");
3679 vTexCoords[3].addModifier("noperspective");
3680 }
3681
Brian Salomon1edc5b92016-11-29 13:43:46 -05003682 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003683 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003684 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003685 vshaderTxt.append(";");
3686 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003687 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003688 vshaderTxt.append(";");
3689 }
3690
3691 vshaderTxt.append(
3692 "// Mipmap Program VS\n"
3693 "void main() {"
3694 " gl_Position.xy = a_vertex * vec2(2, 2) - vec2(1, 1);"
3695 " gl_Position.zw = vec2(0, 1);"
3696 );
3697
3698 // Insert texture coordinate computation:
3699 if (oddWidth && oddHeight) {
3700 vshaderTxt.append(
3701 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
3702 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + vec2(u_texCoordXform.x, 0);"
3703 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + vec2(0, u_texCoordXform.z);"
3704 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3705 );
3706 } else if (oddWidth) {
3707 vshaderTxt.append(
3708 " v_texCoord0 = a_vertex.xy * vec2(u_texCoordXform.y, 1);"
3709 " v_texCoord1 = a_vertex.xy * vec2(u_texCoordXform.y, 1) + vec2(u_texCoordXform.x, 0);"
3710 );
3711 } else if (oddHeight) {
3712 vshaderTxt.append(
3713 " v_texCoord0 = a_vertex.xy * vec2(1, u_texCoordXform.w);"
3714 " v_texCoord1 = a_vertex.xy * vec2(1, u_texCoordXform.w) + vec2(0, u_texCoordXform.z);"
3715 );
3716 } else {
3717 vshaderTxt.append(
3718 " v_texCoord0 = a_vertex.xy;"
3719 );
3720 }
3721
3722 vshaderTxt.append("}");
3723
3724 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003725 if (shaderCaps->noperspectiveInterpolationSupport()) {
3726 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003727 fshaderTxt.appendf("#extension %s : require\n", extension);
3728 }
3729 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003730 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
brianosman33f6b3f2016-06-02 05:49:21 -07003731 &fshaderTxt);
3732 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003733 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003734 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003735 fshaderTxt.append(";");
3736 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003737 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003738 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003739 fshaderTxt.append(
3740 "// Mipmap Program FS\n"
3741 "void main() {"
3742 );
3743
3744 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003745 fshaderTxt.append(
3746 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3747 " texture(u_texture, v_texCoord1) + "
3748 " texture(u_texture, v_texCoord2) + "
3749 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003750 );
3751 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003752 fshaderTxt.append(
3753 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3754 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003755 );
3756 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003757 fshaderTxt.append(
3758 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003759 );
3760 }
3761
3762 fshaderTxt.append("}");
3763
3764 const char* str;
3765 GrGLint length;
3766
3767 str = vshaderTxt.c_str();
3768 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003769 SkSL::Program::Settings settings;
3770 settings.fCaps = shaderCaps;
3771 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003772 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3773 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003774 &fStats, settings, &inputs);
3775 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003776
3777 str = fshaderTxt.c_str();
3778 length = SkToInt(fshaderTxt.size());
3779 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3780 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003781 &fStats, settings, &inputs);
3782 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003783
3784 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3785
3786 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3787 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3788 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3789 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3790
3791 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3792
3793 GL_CALL(DeleteShader(vshader));
3794 GL_CALL(DeleteShader(fshader));
3795
3796 return true;
3797}
3798
cdaltone2e71c22016-04-07 18:13:29 -07003799bool GrGLGpu::createWireRectProgram() {
3800 if (!fWireRectArrayBuffer) {
3801 static const GrGLfloat vdata[] = {
3802 0, 0,
3803 0, 1,
3804 1, 1,
3805 1, 0
3806 };
3807 fWireRectArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3808 kStatic_GrAccessPattern, vdata));
3809 if (!fWireRectArrayBuffer) {
3810 return false;
3811 }
3812 }
3813
bsalomon6dea83f2015-12-03 12:58:06 -08003814 SkASSERT(!fWireRectProgram.fProgram);
cdaltone2e71c22016-04-07 18:13:29 -07003815 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3816 if (!fWireRectProgram.fProgram) {
3817 return false;
3818 }
3819
Brian Salomon99938a82016-11-21 13:41:08 -05003820 GrShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3821 GrShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3822 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003823 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003824
3825 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3826 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3827 // uniform using the 4 vbo vertices.
3828 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003829 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003830 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003831 uRect.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003832 vshaderTxt.append(";");
3833 vshaderTxt.append(
3834 "// Wire Rect Program VS\n"
3835 "void main() {"
3836 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3837 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3838 " gl_Position.zw = vec2(0, 1);"
3839 "}"
3840 );
3841
Brian Salomon99938a82016-11-21 13:41:08 -05003842 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
bsalomon6dea83f2015-12-03 12:58:06 -08003843
3844 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -04003845 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision,
Brian Salomon1edc5b92016-11-29 13:43:46 -05003846 *this->caps()->shaderCaps(),
bsalomon6dea83f2015-12-03 12:58:06 -08003847 &fshaderTxt);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003848 uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003849 fshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003850 fshaderTxt.appendf(
3851 "// Write Rect Program FS\n"
3852 "void main() {"
ethannicholas5961bc92016-10-12 06:39:56 -07003853 " sk_FragColor = %s;"
bsalomon6dea83f2015-12-03 12:58:06 -08003854 "}",
bsalomon6dea83f2015-12-03 12:58:06 -08003855 uColor.c_str()
3856 );
3857
bsalomon6dea83f2015-12-03 12:58:06 -08003858 const char* str;
3859 GrGLint length;
3860
3861 str = vshaderTxt.c_str();
3862 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003863 SkSL::Program::Settings settings;
3864 settings.fCaps = this->caps()->shaderCaps();
3865 SkSL::Program::Inputs inputs;
bsalomon6dea83f2015-12-03 12:58:06 -08003866 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3867 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003868 &fStats, settings, &inputs);
3869 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003870
3871 str = fshaderTxt.c_str();
3872 length = SkToInt(fshaderTxt.size());
3873 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3874 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003875 &fStats, settings, &inputs);
3876 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003877
3878 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3879
3880 GL_CALL_RET(fWireRectProgram.fColorUniform,
3881 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3882 GL_CALL_RET(fWireRectProgram.fRectUniform,
3883 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3884 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3885
3886 GL_CALL(DeleteShader(vshader));
3887 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003888
3889 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003890}
3891
3892void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003893 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3894 // visualization.
3895
bsalomon6dea83f2015-12-03 12:58:06 -08003896 this->handleDirtyContext();
3897 if (!fWireRectProgram.fProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003898 if (!this->createWireRectProgram()) {
3899 SkDebugf("Failed to create wire rect program.\n");
3900 return;
3901 }
bsalomon6dea83f2015-12-03 12:58:06 -08003902 }
3903
3904 int w = rt->width();
3905 int h = rt->height();
3906
3907 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3908 // whether the render target is flipped or not.
3909 GrGLfloat edges[4];
3910 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3911 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3912 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3913 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3914 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3915 } else {
3916 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3917 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3918 }
3919 edges[0] = 2 * edges[0] / w - 1.0f;
3920 edges[1] = 2 * edges[1] / h - 1.0f;
3921 edges[2] = 2 * edges[2] / w - 1.0f;
3922 edges[3] = 2 * edges[3] / h - 1.0f;
3923
3924 GrGLfloat channels[4];
3925 static const GrGLfloat scale255 = 1.f / 255.f;
3926 channels[0] = GrColorUnpackR(color) * scale255;
3927 channels[1] = GrColorUnpackG(color) * scale255;
3928 channels[2] = GrColorUnpackB(color) * scale255;
3929 channels[3] = GrColorUnpackA(color) * scale255;
3930
3931 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3932 this->flushRenderTarget(glRT, &rect);
3933
3934 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3935 fHWProgramID = fWireRectProgram.fProgram;
3936
cdaltone2e71c22016-04-07 18:13:29 -07003937 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003938
cdaltone2e71c22016-04-07 18:13:29 -07003939 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05003940 attribs->set(this, 0, fWireRectArrayBuffer.get(), kVec2f_GrVertexAttribType,
3941 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003942 attribs->disableUnusedArrays(this, 0x1);
3943
3944 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3945 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3946
3947 GrXferProcessor::BlendInfo blendInfo;
3948 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003949 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003950 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07003951 this->flushDrawFace(GrDrawFace::kBoth);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003952 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08003953 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003954 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003955 this->disableStencil();
bsalomon6dea83f2015-12-03 12:58:06 -08003956
3957 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3958}
3959
3960
cdaltone2e71c22016-04-07 18:13:29 -07003961bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07003962 GrSurface* src,
3963 const SkIRect& srcRect,
3964 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003965 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003966 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003967
3968 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003969 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003970 SkDebugf("Failed to create copy program.\n");
3971 return false;
3972 }
3973 }
3974
bsalomon6df86402015-06-01 10:41:49 -07003975 int w = srcRect.width();
3976 int h = srcRect.height();
3977
Brian Salomon514baff2016-11-17 15:17:07 -05003978 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07003979 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003980
bsalomon083617b2016-02-12 12:10:14 -08003981 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003982 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003983 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003984 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003985
bsalomon6df86402015-06-01 10:41:49 -07003986 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003987
bsalomon7ea33f52015-11-22 14:51:00 -08003988 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3989 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003990
cdaltone2e71c22016-04-07 18:13:29 -07003991 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003992
cdaltone2e71c22016-04-07 18:13:29 -07003993 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05003994 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
3995 2 * sizeof(GrGLfloat), 0);
bsalomond6246342015-06-04 13:57:00 -07003996 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003997
3998 // dst rect edges in NDC (-1 to 1)
3999 int dw = dst->width();
4000 int dh = dst->height();
4001 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4002 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4003 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4004 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
4005 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4006 dy0 = -dy0;
4007 dy1 = -dy1;
4008 }
4009
bsalomone5286e02016-01-14 09:24:09 -08004010 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4011 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4012 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4013 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04004014 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07004015 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07004016 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08004017 sy0 = sh - sy0;
4018 sy1 = sh - sy1;
4019 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04004020 // src rect edges in normalized texture space (0 to 1)
4021 sx0 /= sw;
4022 sx1 /= sw;
4023 sy0 /= sh;
4024 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004025
bsalomon7ea33f52015-11-22 14:51:00 -08004026 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4027 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4028 sx1 - sx0, sy1 - sy0, sx0, sy0));
4029 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004030
4031 GrXferProcessor::BlendInfo blendInfo;
4032 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004033 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004034 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004035 this->flushDrawFace(GrDrawFace::kBoth);
bsalomon083617b2016-02-12 12:10:14 -08004036 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004037 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004038 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004039 this->disableStencil();
bsalomon6df86402015-06-01 10:41:49 -07004040
4041 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004042 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08004043 this->didWriteToSurface(dst, &dstRect);
4044
cdaltone2e71c22016-04-07 18:13:29 -07004045 return true;
bsalomon6df86402015-06-01 10:41:49 -07004046}
4047
4048void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
4049 GrSurface* src,
4050 const SkIRect& srcRect,
4051 const SkIPoint& dstPoint) {
4052 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07004053 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004054 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004055 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004056 SkASSERT(dstTex);
4057 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004058 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004059 GrGLIRect srcGLRect;
4060 srcGLRect.setRelativeTo(srcVP,
4061 srcRect.fLeft,
4062 srcRect.fTop,
4063 srcRect.width(),
4064 srcRect.height(),
4065 src->origin());
4066
4067 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004068 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004069 GrGLint dstY;
4070 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4071 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4072 } else {
4073 dstY = dstPoint.fY;
4074 }
bsalomon10528f12015-10-14 12:54:52 -07004075 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004076 dstPoint.fX, dstY,
4077 srcGLRect.fLeft, srcGLRect.fBottom,
4078 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004079 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004080 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4081 srcRect.width(), srcRect.height());
4082 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004083}
4084
4085bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
4086 GrSurface* src,
4087 const SkIRect& srcRect,
4088 const SkIPoint& dstPoint) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04004089 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07004090 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4091 srcRect.width(), srcRect.height());
4092 if (dst == src) {
4093 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4094 return false;
mtklein404b3b22015-05-18 09:29:10 -07004095 }
bsalomon5df6fee2015-05-18 06:26:15 -07004096 }
bsalomon6df86402015-06-01 10:41:49 -07004097
bsalomon6df86402015-06-01 10:41:49 -07004098 GrGLIRect dstVP;
4099 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004100 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4101 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004102 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004103 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004104 GrGLIRect srcGLRect;
4105 GrGLIRect dstGLRect;
4106 srcGLRect.setRelativeTo(srcVP,
4107 srcRect.fLeft,
4108 srcRect.fTop,
4109 srcRect.width(),
4110 srcRect.height(),
4111 src->origin());
4112 dstGLRect.setRelativeTo(dstVP,
4113 dstRect.fLeft,
4114 dstRect.fTop,
4115 dstRect.width(),
4116 dstRect.height(),
4117 dst->origin());
4118
4119 // BlitFrameBuffer respects the scissor, so disable it.
4120 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004121 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004122
4123 GrGLint srcY0;
4124 GrGLint srcY1;
4125 // Does the blit need to y-mirror or not?
4126 if (src->origin() == dst->origin()) {
4127 srcY0 = srcGLRect.fBottom;
4128 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4129 } else {
4130 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4131 srcY1 = srcGLRect.fBottom;
4132 }
4133 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4134 srcY0,
4135 srcGLRect.fLeft + srcGLRect.fWidth,
4136 srcY1,
4137 dstGLRect.fLeft,
4138 dstGLRect.fBottom,
4139 dstGLRect.fLeft + dstGLRect.fWidth,
4140 dstGLRect.fBottom + dstGLRect.fHeight,
4141 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004142 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4143 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004144 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004145 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004146}
4147
brianosman33f6b3f2016-06-02 05:49:21 -07004148// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4149// Uses draw calls to do a series of downsample operations to successive mips.
4150// If this returns false, then the calling code falls back to using glGenerateMipmap.
4151bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004152 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004153 // Our iterative downsample requires the ability to limit which level we're sampling:
4154 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004155 return false;
4156 }
4157
4158 // Mipmaps are only supported on 2D textures:
4159 if (GR_GL_TEXTURE_2D != texture->target()) {
4160 return false;
4161 }
4162
4163 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004164 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004165 return false;
4166 }
4167
brianosman33f6b3f2016-06-02 05:49:21 -07004168 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4169 if (GrPixelConfigIsSRGB(texture->config())) {
4170 // If we have write-control, just set the state that we want:
4171 if (this->glCaps().srgbWriteControl()) {
4172 this->flushFramebufferSRGB(gammaCorrect);
4173 } else if (!gammaCorrect) {
4174 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4175 return false;
4176 }
4177 }
4178
4179 int width = texture->width();
4180 int height = texture->height();
4181 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4182
4183 // Define all mips, if we haven't previously done so:
4184 if (0 == texture->texturePriv().maxMipMapLevel()) {
4185 GrGLenum internalFormat;
4186 GrGLenum externalFormat;
4187 GrGLenum externalType;
4188 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4189 &internalFormat, &externalFormat, &externalType)) {
4190 return false;
4191 }
4192
4193 for (GrGLint level = 1; level < levelCount; ++level) {
4194 // Define the next mip:
4195 width = SkTMax(1, width / 2);
4196 height = SkTMax(1, height / 2);
4197 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4198 width, height, 0,
4199 externalFormat, externalType, nullptr));
4200 }
4201 }
4202
4203 // Create (if necessary), then bind temporary FBO:
4204 if (0 == fTempDstFBOID) {
4205 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4206 }
4207 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004208 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004209
4210 // Bind the texture, to get things configured for filtering.
4211 // We'll be changing our base level further below:
4212 this->setTextureUnit(0);
Brian Salomon514baff2016-11-17 15:17:07 -05004213 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
brianosman33f6b3f2016-06-02 05:49:21 -07004214 this->bindTexture(0, params, gammaCorrect, texture);
4215
4216 // Vertex data:
4217 if (!fMipmapProgramArrayBuffer) {
4218 static const GrGLfloat vdata[] = {
4219 0, 0,
4220 0, 1,
4221 1, 0,
4222 1, 1
4223 };
4224 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4225 kVertex_GrBufferType,
4226 kStatic_GrAccessPattern, vdata));
4227 }
4228 if (!fMipmapProgramArrayBuffer) {
4229 return false;
4230 }
4231
4232 fHWVertexArrayState.setVertexArrayID(this, 0);
4233
4234 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05004235 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004236 2 * sizeof(GrGLfloat), 0);
4237 attribs->disableUnusedArrays(this, 0x1);
4238
4239 // Set "simple" state once:
4240 GrXferProcessor::BlendInfo blendInfo;
4241 blendInfo.reset();
4242 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4243 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004244 this->flushDrawFace(GrDrawFace::kBoth);
brianosman33f6b3f2016-06-02 05:49:21 -07004245 this->flushHWAAState(nullptr, false, false);
4246 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004247 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004248 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004249
4250 // Do all the blits:
4251 width = texture->width();
4252 height = texture->height();
4253 GrGLIRect viewport;
4254 viewport.fLeft = 0;
4255 viewport.fBottom = 0;
4256 for (GrGLint level = 1; level < levelCount; ++level) {
4257 // Get and bind the program for this particular downsample (filter shape can vary):
4258 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4259 if (!fMipmapPrograms[progIdx].fProgram) {
4260 if (!this->createMipmapProgram(progIdx)) {
4261 SkDebugf("Failed to create mipmap program.\n");
4262 return false;
4263 }
4264 }
4265 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4266 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4267
4268 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4269 const float invWidth = 1.0f / width;
4270 const float invHeight = 1.0f / height;
4271 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4272 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4273 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4274
4275 // Only sample from previous mip
4276 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4277
4278 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4279 GR_GL_TEXTURE_2D, texture->textureID(), level));
4280
4281 width = SkTMax(1, width / 2);
4282 height = SkTMax(1, height / 2);
4283 viewport.fWidth = width;
4284 viewport.fHeight = height;
4285 this->flushViewport(viewport);
4286
4287 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4288 }
4289
4290 // Unbind:
4291 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4292 GR_GL_TEXTURE_2D, 0, 0));
4293
4294 return true;
4295}
4296
csmartdaltonc25c5d72016-11-01 07:03:59 -07004297void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
4298 int* effectiveSampleCnt, SamplePattern* samplePattern) {
csmartdaltonf9635992016-08-10 11:09:07 -07004299 SkASSERT(!rt->isMixedSampled() || rt->renderTargetPriv().getStencilAttachment() ||
cdalton28f45b92016-03-07 13:58:26 -08004300 stencil.isDisabled());
4301
4302 this->flushStencil(stencil);
4303 this->flushHWAAState(rt, true, !stencil.isDisabled());
4304 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4305
4306 if (0 != this->caps()->maxRasterSamples()) {
4307 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4308 } else {
4309 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4310 }
4311
4312 SkASSERT(*effectiveSampleCnt >= rt->desc().fSampleCnt);
4313
4314 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004315 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004316 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4317 GrGLfloat pos[2];
4318 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4319 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004320 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004321 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004322 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004323 }
4324 }
4325 }
4326}
4327
cdalton231c5fd2015-05-13 12:35:36 -07004328void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004329 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004330 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004331 case kTexture_GrXferBarrierType: {
4332 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4333 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4334 // The render target uses separate storage so no need for glTextureBarrier.
4335 // FIXME: The render target will resolve automatically when its texture is bound,
4336 // but we could resolve only the bounds that will be read if we do it here instead.
4337 return;
4338 }
cdalton9954bc32015-04-29 14:17:00 -07004339 SkASSERT(this->caps()->textureBarrierSupport());
4340 GL_CALL(TextureBarrier());
4341 return;
cdalton231c5fd2015-05-13 12:35:36 -07004342 }
cdalton8917d622015-05-06 13:40:21 -07004343 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004344 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004345 this->caps()->blendEquationSupport());
4346 GL_CALL(BlendBarrier());
4347 return;
bsalomoncb02b382015-08-12 11:14:50 -07004348 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004349 }
4350}
4351
jvanverth88957922015-07-14 11:02:52 -07004352GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004353 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004354 if (!this->caps()->isConfigTexturable(config)) {
4355 return false;
4356 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004357 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004358 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004359 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004360 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004361 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4362 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004363 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004364 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004365 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4366 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4367 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4368 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004369
bsalomon76148af2016-01-12 11:13:47 -08004370 GrGLenum internalFormat;
4371 GrGLenum externalFormat;
4372 GrGLenum externalType;
4373
4374 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4375 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004376 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004377 }
jvanverth672bb7f2015-07-13 07:19:57 -07004378
bsalomon091f60c2015-11-10 11:54:56 -08004379 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004380 externalType, pixels));
4381
Ben Wagner18b61f92016-10-25 10:44:02 -04004382 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004383}
4384
jvanverth88957922015-07-14 11:02:52 -07004385bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004386 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004387
4388 GrGLboolean result;
4389 GL_CALL_RET(result, IsTexture(texID));
4390
4391 return (GR_GL_TRUE == result);
4392}
4393
bsalomone63ffef2016-02-05 07:17:34 -08004394void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004395 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004396 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004397
bsalomon67d76202015-11-11 12:40:42 -08004398 if (!abandonTexture) {
4399 GL_CALL(DeleteTextures(1, &texID));
4400 }
jvanverth672bb7f2015-07-13 07:19:57 -07004401}
4402
joshualitt8fd844f2015-12-02 13:36:47 -08004403void GrGLGpu::resetShaderCacheForTesting() const {
4404 fProgramCache->abandon();
4405}
4406
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004407///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004408
cdaltone2e71c22016-04-07 18:13:29 -07004409GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004410 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004411 GrGLAttribArrayState* attribState;
4412
cdaltone2e71c22016-04-07 18:13:29 -07004413 if (gpu->glCaps().isCoreProfile()) {
4414 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004415 GrGLuint arrayID;
4416 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4417 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004418 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004419 }
cdaltone2e71c22016-04-07 18:13:29 -07004420 if (ibuf) {
4421 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004422 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004423 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004424 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004425 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004426 if (ibuf) {
4427 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4428 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004429 } else {
4430 this->setVertexArrayID(gpu, 0);
4431 }
4432 int attrCount = gpu->glCaps().maxVertexAttributes();
4433 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4434 fDefaultVertexArrayAttribState.resize(attrCount);
4435 }
4436 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004437 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004438 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004439}
bsalomone179a912016-01-20 06:18:10 -08004440
Robert Phillips3798c862017-03-27 11:08:16 -04004441bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Robert Phillips81444fb2017-03-21 09:14:35 -04004442 const GrSamplerParams& textureParams,
4443 GrTextureProducer::CopyParams* copyParams,
4444 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004445 const GrTexture* texture = proxy->priv().peekTexture();
4446 if (!texture) {
4447 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4448 // In that case the proxy should already be instantiated.
4449 return false;
4450 }
4451
bsalomone179a912016-01-20 06:18:10 -08004452 if (textureParams.isTiled() ||
Brian Salomon514baff2016-11-17 15:17:07 -05004453 GrSamplerParams::kMipMap_FilterMode == textureParams.filterMode()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004454 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004455 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4456 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon514baff2016-11-17 15:17:07 -05004457 copyParams->fFilter = GrSamplerParams::kNone_FilterMode;
bsalomone179a912016-01-20 06:18:10 -08004458 copyParams->fWidth = texture->width();
4459 copyParams->fHeight = texture->height();
4460 return true;
4461 }
4462 }
4463 return false;
4464}
jvanverth84741b32016-09-30 08:39:02 -07004465
Greg Daniel6be35232017-03-01 17:01:09 -05004466GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
4467 GrGLsync sync;
4468 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4469 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4470 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004471}
4472
Greg Daniel6be35232017-03-01 17:01:09 -05004473bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004474 GrGLenum result;
4475 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4476 return (GR_GL_CONDITION_SATISFIED == result);
4477}
4478
4479void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004480 this->deleteSync((GrGLsync)fence);
4481}
4482
4483sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore() {
4484 return GrGLSemaphore::Make(this);
4485}
4486
Brian Osmandc87c952017-04-28 13:57:38 -04004487void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004488 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4489
4490 GrGLsync sync;
4491 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4492 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004493
4494 if (flush) {
4495 GL_CALL(Flush());
4496 }
Greg Daniel6be35232017-03-01 17:01:09 -05004497}
4498
4499void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4500 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4501
4502 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4503}
4504
4505void GrGLGpu::deleteSync(GrGLsync sync) const {
4506 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004507}