blob: b324280895f70f3973cf963dc35b4d5e68c3cd92 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
Brian Salomon028a9a52017-05-11 11:39:08 -040010#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "../private/GrGLSL.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050013#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070014#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070015#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050016#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080018#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080019#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070020#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050021#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080022#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050023#include "GrShaderCaps.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "GrSurfacePriv.h"
Robert Phillips3798c862017-03-27 11:08:16 -040025#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070026#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000027#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000029#include "SkMakeUnique.h"
30#include "SkMipMap.h"
31#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000032#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050033#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000034#include "SkTemplates.h"
35#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050036#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050037#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000038
bsalomon@google.com0b77d682011-08-19 13:28:54 +000039#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000040#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000041
reed@google.comac10a2d2010-12-22 21:39:39 +000042#define SKIP_CACHE_CHECK true
43
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000044#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
45 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
46 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
47 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000049 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
50 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
51 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
52#endif
53
Jim Van Verth32ac83e2016-11-28 15:23:57 -050054//#define USE_NSIGHT
55
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000056///////////////////////////////////////////////////////////////////////////////
57
csmartdaltona7f29642016-07-07 08:49:11 -070058using gr_instanced::InstancedRendering;
59using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070060
Robert Phillipse3302df2017-04-24 07:31:02 -040061using gr_instanced::OpAllocator;
62using gr_instanced::GLOpAllocator;
63
cdalton8917d622015-05-06 13:40:21 -070064static const GrGLenum gXfermodeEquation2Blend[] = {
65 // Basic OpenGL blend equations.
66 GR_GL_FUNC_ADD,
67 GR_GL_FUNC_SUBTRACT,
68 GR_GL_FUNC_REVERSE_SUBTRACT,
69
70 // GL_KHR_blend_equation_advanced.
71 GR_GL_SCREEN,
72 GR_GL_OVERLAY,
73 GR_GL_DARKEN,
74 GR_GL_LIGHTEN,
75 GR_GL_COLORDODGE,
76 GR_GL_COLORBURN,
77 GR_GL_HARDLIGHT,
78 GR_GL_SOFTLIGHT,
79 GR_GL_DIFFERENCE,
80 GR_GL_EXCLUSION,
81 GR_GL_MULTIPLY,
82 GR_GL_HSL_HUE,
83 GR_GL_HSL_SATURATION,
84 GR_GL_HSL_COLOR,
85 GR_GL_HSL_LUMINOSITY
86};
87GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
88GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
90GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
91GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
92GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
93GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
94GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
95GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
96GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
97GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
98GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
99GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
100GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
101GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
102GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
103GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
104GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700105GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107static const GrGLenum gXfermodeCoeff2Blend[] = {
108 GR_GL_ZERO,
109 GR_GL_ONE,
110 GR_GL_SRC_COLOR,
111 GR_GL_ONE_MINUS_SRC_COLOR,
112 GR_GL_DST_COLOR,
113 GR_GL_ONE_MINUS_DST_COLOR,
114 GR_GL_SRC_ALPHA,
115 GR_GL_ONE_MINUS_SRC_ALPHA,
116 GR_GL_DST_ALPHA,
117 GR_GL_ONE_MINUS_DST_ALPHA,
118 GR_GL_CONSTANT_COLOR,
119 GR_GL_ONE_MINUS_CONSTANT_COLOR,
120 GR_GL_CONSTANT_ALPHA,
121 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000128};
129
bsalomon861e1032014-12-16 07:33:49 -0800130bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000131 static const bool gCoeffReferencesBlendConst[] = {
132 false,
133 false,
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 true,
143 true,
144 true,
145 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000146
147 // extended blend coeffs
148 false,
149 false,
150 false,
151 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000152 };
153 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700154 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155
bsalomon@google.com47059542012-06-06 20:51:20 +0000156 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
157 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
158 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
159 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
160 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
161 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
162 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
163 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
164 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
165 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
166 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
167 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
168 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
169 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000170
bsalomon@google.com47059542012-06-06 20:51:20 +0000171 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
172 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
173 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
174 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000175
176 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700177 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000178}
179
reed@google.comac10a2d2010-12-22 21:39:39 +0000180///////////////////////////////////////////////////////////////////////////////
181
egdanielff1d5472015-09-10 08:37:20 -0700182
bsalomon682c2692015-05-22 14:01:46 -0700183GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
184 GrContext* context) {
Hal Canary144caf52016-11-07 17:57:18 -0500185 sk_sp<const GrGLInterface> glInterface(
bsalomon424cc262015-05-22 10:37:30 -0700186 reinterpret_cast<const GrGLInterface*>(backendContext));
187 if (!glInterface) {
188 glInterface.reset(GrGLDefaultInterface());
189 } else {
190 glInterface->ref();
191 }
192 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700193 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700194 }
Jim Van Verth76334772017-05-05 16:46:05 -0400195#ifdef USE_NSIGHT
196 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
197#endif
Hal Canary144caf52016-11-07 17:57:18 -0500198 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700199 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700200 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700201 }
halcanary96fcdcc2015-08-27 07:41:13 -0700202 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700203}
204
rileya@google.come38160c2012-07-03 18:03:04 +0000205static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000206
bsalomon424cc262015-05-22 10:37:30 -0700207GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000208 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700209 , fGLContext(ctx)
210 , fProgramCache(new ProgramCache(this))
211 , fHWProgramID(0)
212 , fTempSrcFBOID(0)
213 , fTempDstFBOID(0)
214 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700215 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700216 , fHWMinSampleShading(0.0) {
217 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
218 fCopyPrograms[i].fProgram = 0;
219 }
brianosman33f6b3f2016-06-02 05:49:21 -0700220 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
221 fMipmapPrograms[i].fProgram = 0;
222 }
cdaltone2e71c22016-04-07 18:13:29 -0700223 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700224
bsalomon424cc262015-05-22 10:37:30 -0700225 SkASSERT(ctx);
226 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000227
Brian Salomon1edc5b92016-11-29 13:43:46 -0500228 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
229 fHWBoundImageStorages.reset(this->caps()->shaderCaps()->maxCombinedImageStorages());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000230
cdaltone2e71c22016-04-07 18:13:29 -0700231 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
232 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
233 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
234 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
235 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
236 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
237 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
238 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
239 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
240 } else {
241 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
242 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
243 }
244 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
245
cdalton74b8d322016-04-11 14:47:28 -0700246 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500247 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700248 }
249
cdaltone2e71c22016-04-07 18:13:29 -0700250 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
251 fPathRendering.reset(new GrGLPathRendering(this));
252 }
253
bsalomon424cc262015-05-22 10:37:30 -0700254 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000255 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000256 const GrGLubyte* vendor;
257 const GrGLubyte* renderer;
258 const GrGLubyte* version;
259 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
260 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
261 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800262 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000263 this);
tfarina38406c82014-10-31 07:11:12 -0700264 SkDebugf("------ VENDOR %s\n", vendor);
265 SkDebugf("------ RENDERER %s\n", renderer);
266 SkDebugf("------ VERSION %s\n", version);
267 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700268 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700269 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800270 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
bsalomon861e1032014-12-16 07:33:49 -0800274GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700275 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
276 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800277 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700278 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700279 fMipmapProgramArrayBuffer.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700280 fWireRectArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800281
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000282 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000283 // detach the current program so there is no confusion on OpenGL's part
284 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000285 GL_CALL(UseProgram(0));
286 }
287
egdanield803f272015-03-18 13:01:52 -0700288 if (0 != fTempSrcFBOID) {
289 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800290 }
egdanield803f272015-03-18 13:01:52 -0700291 if (0 != fTempDstFBOID) {
292 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800293 }
egdanield803f272015-03-18 13:01:52 -0700294 if (0 != fStencilClearFBOID) {
295 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800296 }
egdaniel0f5f9672015-02-03 11:10:51 -0800297
bsalomon7ea33f52015-11-22 14:51:00 -0800298 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
299 if (0 != fCopyPrograms[i].fProgram) {
300 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
301 }
bsalomon6df86402015-06-01 10:41:49 -0700302 }
bsalomon6dea83f2015-12-03 12:58:06 -0800303
brianosman33f6b3f2016-06-02 05:49:21 -0700304 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
305 if (0 != fMipmapPrograms[i].fProgram) {
306 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
307 }
308 }
309
bsalomon6dea83f2015-12-03 12:58:06 -0800310 if (0 != fWireRectProgram.fProgram) {
311 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
312 }
313
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000314 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700315}
316
bsalomon6e2aad42016-04-01 11:54:31 -0700317void GrGLGpu::disconnect(DisconnectType type) {
318 INHERITED::disconnect(type);
319 if (DisconnectType::kCleanup == type) {
320 if (fHWProgramID) {
321 GL_CALL(UseProgram(0));
322 }
323 if (fTempSrcFBOID) {
324 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
325 }
326 if (fTempDstFBOID) {
327 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
328 }
329 if (fStencilClearFBOID) {
330 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
331 }
bsalomon6e2aad42016-04-01 11:54:31 -0700332 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
333 if (fCopyPrograms[i].fProgram) {
334 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
335 }
336 }
brianosman33f6b3f2016-06-02 05:49:21 -0700337 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
338 if (fMipmapPrograms[i].fProgram) {
339 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
340 }
341 }
bsalomon6e2aad42016-04-01 11:54:31 -0700342 if (fWireRectProgram.fProgram) {
343 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
344 }
bsalomon6e2aad42016-04-01 11:54:31 -0700345 } else {
346 if (fProgramCache) {
347 fProgramCache->abandon();
348 }
349 }
350
351 delete fProgramCache;
352 fProgramCache = nullptr;
353
bsalomonc8dc1f72014-08-21 13:02:13 -0700354 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700355 fTempSrcFBOID = 0;
356 fTempDstFBOID = 0;
357 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700358 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800359 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
360 fCopyPrograms[i].fProgram = 0;
361 }
brianosman33f6b3f2016-06-02 05:49:21 -0700362 fMipmapProgramArrayBuffer.reset();
363 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
364 fMipmapPrograms[i].fProgram = 0;
365 }
bsalomon6dea83f2015-12-03 12:58:06 -0800366 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700367 fWireRectArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700368 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700369 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700370 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000371}
372
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000373///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000374
bsalomon861e1032014-12-16 07:33:49 -0800375void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000376 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400377 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000378 GL_CALL(Disable(GR_GL_DEPTH_TEST));
379 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000380
Brian Salomonf0861672017-05-08 11:10:10 -0400381 // We don't use face culling.
382 GL_CALL(Disable(GR_GL_CULL_FACE));
383 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
384 // just set this to the default for self-consistency.
385 GL_CALL(FrontFace(GR_GL_CCW));
386
cdaltone2e71c22016-04-07 18:13:29 -0700387 fHWBufferState[kTexel_GrBufferType].invalidate();
388 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
389 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
390 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700391
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000392 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500393#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000394 // Desktop-only state that we never change
395 if (!this->glCaps().isCoreProfile()) {
396 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
397 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
398 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
399 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
400 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
401 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
402 }
403 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
404 // core profile. This seems like a bug since the core spec removes any mention of
405 // GL_ARB_imaging.
406 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
407 GL_CALL(Disable(GR_GL_COLOR_TABLE));
408 }
409 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400410
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400411 if (this->caps()->wireframeMode()) {
412 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
413 } else {
414 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
415 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500416#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000417 // Since ES doesn't support glPointSize at all we always use the VS to
418 // set the point size
419 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
420
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000421 }
joshualitt58162332014-08-01 06:44:53 -0700422
423 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700424 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700425 // The arm extension requires specifically enabling MSAA fetching per sample.
426 // On some devices this may have a perf hit. Also multiple render targets are disabled
427 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
428 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000429 fHWWriteToColor = kUnknown_TriState;
430 // we only ever use lines in hairline mode
431 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700432 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000433 }
edisonn@google.comba669992013-06-28 16:03:21 +0000434
egdanielb414f252014-07-29 13:15:47 -0700435 if (resetBits & kMSAAEnable_GrGLBackendState) {
436 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700437
egdanieleed519e2016-01-15 11:36:18 -0800438 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800439 if (0 != this->caps()->maxRasterSamples()) {
440 fHWRasterMultisampleEnabled = kUnknown_TriState;
441 fHWNumRasterSamples = 0;
442 }
443
444 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
445 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700446 GL_CALL(CoverageModulation(GR_GL_RGBA));
447 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000448 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000450 fHWActiveTextureUnitIdx = -1; // invalid
451
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000452 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700453 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500454 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000455 }
cdalton74b8d322016-04-11 14:47:28 -0700456 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
457 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
458 fHWBufferTextures[b].fKnownBound = false;
459 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500460 for (int i = 0; i < fHWBoundImageStorages.count(); ++i) {
Brian Salomon8b7a7dd2016-11-29 15:29:41 -0500461 SkASSERT(this->caps()->shaderCaps()->imageLoadStoreSupport());
Brian Salomonf9f45122016-11-29 11:59:17 -0500462 fHWBoundImageStorages[i].fTextureUniqueID.makeInvalid();
463 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000464 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000465
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000466 if (resetBits & kBlend_GrGLBackendState) {
467 fHWBlendState.invalidate();
468 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000469
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000470 if (resetBits & kView_GrGLBackendState) {
471 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700472 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000473 fHWViewport.invalidate();
474 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000476 if (resetBits & kStencil_GrGLBackendState) {
477 fHWStencilSettings.invalidate();
478 fHWStencilTestEnabled = kUnknown_TriState;
479 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000480
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000481 // Vertex
482 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700483 fHWVertexArrayState.invalidate();
484 fHWBufferState[kVertex_GrBufferType].invalidate();
485 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000486 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000487
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000488 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500489 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700490 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000491 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000492
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000493 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700494 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700495 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000496 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000497 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000498
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000499 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000500 if (resetBits & kPixelStore_GrGLBackendState) {
501 if (this->glCaps().unpackRowLengthSupport()) {
502 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
503 }
504 if (this->glCaps().packRowLengthSupport()) {
505 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
506 }
507 if (this->glCaps().unpackFlipYSupport()) {
508 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
509 }
510 if (this->glCaps().packFlipYSupport()) {
511 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
512 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000513 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000514
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000515 if (resetBits & kProgram_GrGLBackendState) {
516 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000517 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000518}
519
Greg Daniel7ef28f32017-04-20 16:41:55 +0000520sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
521 GrSurfaceOrigin origin,
522 GrBackendTextureFlags flags,
523 int sampleCnt,
bungeman6bd52842016-10-27 09:30:08 -0700524 GrWrapOwnership ownership) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000525 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800526 if (!info || !info->fID) {
527 return nullptr;
528 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000529
Greg Daniel7ef28f32017-04-20 16:41:55 +0000530 // next line relies on GrBackendTextureFlags matching GrTexture's
531 bool renderTarget = SkToBool(flags & kRenderTarget_GrBackendTextureFlag);
bsalomon7ea33f52015-11-22 14:51:00 -0800532
bsalomonb15b4c12014-10-29 12:41:57 -0700533 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -0800534 idDesc.fInfo = *info;
bsalomone5286e02016-01-14 09:24:09 -0800535
bsalomon7ea33f52015-11-22 14:51:00 -0800536 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
537 if (renderTarget) {
538 // This combination is not supported.
539 return nullptr;
540 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500541 if (!this->caps()->shaderCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800542 return nullptr;
543 }
bsalomone5286e02016-01-14 09:24:09 -0800544 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
545 if (!this->glCaps().rectangleTextureSupport()) {
546 return nullptr;
547 }
548 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
549 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800550 }
bsalomone5286e02016-01-14 09:24:09 -0800551
552 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800553 // for a render buffer that resolves to the texture. We don't support MSAA textures.
Greg Daniel7ef28f32017-04-20 16:41:55 +0000554 if (sampleCnt && !renderTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800555 return nullptr;
556 }
bsalomon10528f12015-10-14 12:54:52 -0700557
Brian Osman766fcbb2017-03-13 09:33:09 -0400558 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000559 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400560 } else {
561 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800562 }
bsalomonb15b4c12014-10-29 12:41:57 -0700563
Ben Wagner18b61f92016-10-25 10:44:02 -0400564 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000565 surfDesc.fFlags = (GrSurfaceFlags) flags;
566 surfDesc.fWidth = backendTex.width();
567 surfDesc.fHeight = backendTex.height();
568 surfDesc.fConfig = backendTex.config();
569 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000570 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
571 // assuming the old behaviour, which is that backend textures are always
572 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
573 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000574 if (kDefault_GrSurfaceOrigin == origin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700575 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000576 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000577 surfDesc.fOrigin = origin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000578 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000579
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000580 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700581 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700582 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700583 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000584 }
bungeman6bd52842016-10-27 09:30:08 -0700585 return GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000586 }
Brian Osman766fcbb2017-03-13 09:33:09 -0400587
Brian Osman85d34b22017-05-10 12:06:26 -0400588 return GrGLTexture::MakeWrapped(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000589}
590
Greg Danielbcf612b2017-05-01 13:50:58 +0000591sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
592 GrSurfaceOrigin origin) {
593 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
594 if (!info) {
595 return nullptr;
596 }
597
bsalomonb15b4c12014-10-29 12:41:57 -0700598 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000599 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700600 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700601 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500602 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700603 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000604
bsalomonb15b4c12014-10-29 12:41:57 -0700605 GrSurfaceDesc desc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000606 desc.fConfig = backendRT.config();
Brian Salomon0ec981b2017-05-15 13:48:50 -0400607 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000608 desc.fWidth = backendRT.width();
609 desc.fHeight = backendRT.height();
610 desc.fSampleCnt = SkTMin(backendRT.sampleCnt(), this->caps()->maxSampleCount());
611 SkASSERT(kDefault_GrSurfaceOrigin != origin);
612 desc.fOrigin = origin;
bsalomonb15b4c12014-10-29 12:41:57 -0700613
Greg Danielbcf612b2017-05-01 13:50:58 +0000614 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000615}
616
Greg Daniel7ef28f32017-04-20 16:41:55 +0000617sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
618 GrSurfaceOrigin origin,
619 int sampleCnt) {
620 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800621 if (!info || !info->fID) {
622 return nullptr;
623 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800624
jvanverthdb1adf52016-05-03 05:37:45 -0700625 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700626 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800627
jvanverthdb1adf52016-05-03 05:37:45 -0700628 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
629 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800630 // Only texture rectangle and texture 2d are supported. We do not check whether texture
631 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
632 // we assume the necessary support exists.
633 return nullptr;
634 }
635
Ben Wagner18b61f92016-10-25 10:44:02 -0400636 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000637 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
638 surfDesc.fWidth = tex.width();
639 surfDesc.fHeight = tex.height();
640 surfDesc.fConfig = tex.config();
641 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
ericrkf7b8b8a2016-02-24 14:49:51 -0800642 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
643 // assuming the old behaviour, which is that backend textures are always
644 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
645 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000646 if (kDefault_GrSurfaceOrigin == origin) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800647 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
648 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000649 surfDesc.fOrigin = origin;
ericrkf7b8b8a2016-02-24 14:49:51 -0800650 }
651
652 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700653 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800654 return nullptr;
655 }
bungeman6bd52842016-10-27 09:30:08 -0700656 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800657}
658
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000659////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800660
bsalomonf0674512015-07-28 13:26:15 -0700661bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800662 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700663 DrawPreference* drawPreference,
664 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Osmand0be1ef2017-01-11 16:57:15 -0500665 if (GrPixelConfigIsCompressed(dstSurface->config())) {
bsalomonf0674512015-07-28 13:26:15 -0700666 return false;
667 }
668
bsalomon6cb3cbe2015-07-30 07:34:27 -0700669 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
670 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
671 if (!dstSurface->asTexture()) {
672 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800673 } else {
674 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800675 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
676 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800677 return false;
678 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700679 }
680
Brian Osman33910292017-04-18 14:38:53 -0400681 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
682 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
683 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
684 }
685
bsalomon16921ec2015-07-30 15:34:56 -0700686 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
687 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
688 }
689
bsalomon6c9cd552016-01-22 07:17:34 -0800690 // Start off assuming no swizzling
691 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
692 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700693
694 // These settings we will always want if a temp draw is performed. Initially set the config
695 // to srcConfig, though that may be modified if we decide to do a R/G swap.
696 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
697 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
698 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
699 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
700 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
701 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
702
703 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
704
705 if (configsAreRBSwaps) {
706 if (!this->caps()->isConfigTexturable(srcConfig)) {
707 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
708 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800709 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
710 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700711 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
712 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700713 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
714 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800715 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
716 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700717 } else if (kGLES_GrGLStandard == this->glStandard() &&
718 this->glCaps().bgraIsInternalFormat()) {
719 // The internal format and external formats must match texture uploads so we can't
720 // swizzle while uploading when BGRA is a distinct internal format.
721 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
722 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800723 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
724 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700725 }
726 }
727
728 if (!this->glCaps().unpackFlipYSupport() &&
729 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
730 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
731 }
732
733 return true;
734}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735
jvanverth17aa0472016-01-05 10:41:27 -0800736static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
737 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700738 if (!glTex) {
739 return false;
740 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000741
bsalomon16921ec2015-07-30 15:34:56 -0700742 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
743 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
744 return false;
745 }
746
bsalomone5286e02016-01-14 09:24:09 -0800747 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
748 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800749 return false;
750 }
751
jvanverth17aa0472016-01-05 10:41:27 -0800752 return true;
753}
754
755bool GrGLGpu::onWritePixels(GrSurface* surface,
756 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800757 GrPixelConfig config,
758 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800759 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
760
761 if (!check_write_and_transfer_input(glTex, surface, config)) {
762 return false;
763 }
764
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000765 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700766 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000767
krajcevski145d48c2014-06-11 16:07:50 -0700768 bool success = false;
Brian Salomon154ce912017-05-17 10:40:02 -0400769 if (GrPixelConfigIsCompressed(glTex->config())) {
bsalomon861e1032014-12-16 07:33:49 -0800770 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
Brian Salomon154ce912017-05-17 10:40:02 -0400771 SkASSERT(config == glTex->config());
Brian Salomond1eaf492017-05-18 10:02:08 -0400772 success = this->uploadCompressedTexData(glTex->config(), glTex->width(), glTex->height(),
773 glTex->origin(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800774 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700775 } else {
Brian Salomond1eaf492017-05-18 10:02:08 -0400776 success = this->uploadTexData(glTex->config(), glTex->width(), glTex->height(),
777 glTex->origin(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800778 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700779 }
780
cblume55f2d2d2016-02-26 13:20:48 -0800781 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000782}
783
jvanverthc3d706f2016-04-20 10:33:27 -0700784bool GrGLGpu::onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800785 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700786 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800787 size_t offset, size_t rowBytes) {
jvanverthc3d706f2016-04-20 10:33:27 -0700788 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800789
jvanverthc3d706f2016-04-20 10:33:27 -0700790 if (!check_write_and_transfer_input(glTex, surface, config)) {
jvanverth17aa0472016-01-05 10:41:27 -0800791 return false;
792 }
793
794 // For the moment, can't transfer compressed data
Brian Salomon154ce912017-05-17 10:40:02 -0400795 if (GrPixelConfigIsCompressed(glTex->config())) {
jvanverth17aa0472016-01-05 10:41:27 -0800796 return false;
797 }
798
799 this->setScratchTextureUnit();
800 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
801
cdalton397536c2016-03-25 12:15:03 -0700802 SkASSERT(!transferBuffer->isMapped());
803 SkASSERT(!transferBuffer->isCPUBacked());
804 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700805 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800806
807 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800808 GrMipLevel mipLevel;
jvanverthc3d706f2016-04-20 10:33:27 -0700809 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800810 mipLevel.fRowBytes = rowBytes;
811 SkSTArray<1, GrMipLevel> texels;
812 texels.push_back(mipLevel);
Brian Salomond1eaf492017-05-18 10:02:08 -0400813 success = this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->origin(),
814 glTex->target(), kTransfer_UploadType, left, top, width, height,
815 config, texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700816 return success;
jvanverth17aa0472016-01-05 10:41:27 -0800817}
818
bsalomonf46a1242015-12-15 12:37:38 -0800819// For GL_[UN]PACK_ALIGNMENT.
820static inline GrGLint config_alignment(GrPixelConfig config) {
821 SkASSERT(!GrPixelConfigIsCompressed(config));
822 switch (config) {
823 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500824 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800825 return 1;
826 case kRGB_565_GrPixelConfig:
827 case kRGBA_4444_GrPixelConfig:
828 case kAlpha_half_GrPixelConfig:
829 case kRGBA_half_GrPixelConfig:
830 return 2;
831 case kRGBA_8888_GrPixelConfig:
832 case kBGRA_8888_GrPixelConfig:
833 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700834 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500835 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800836 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500837 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800838 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500839 case kUnknown_GrPixelConfig:
840 case kETC1_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800841 return 0;
842 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500843 SkFAIL("Invalid pixel config");
844 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800845}
846
cblume55f2d2d2016-02-26 13:20:48 -0800847/**
848 * Creates storage space for the texture and fills it with texels.
849 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400850 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800851 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800852 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700853 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
854 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800855 * @param externalFormat The data format used for the external storage of the texture.
856 * @param externalType The type of the data used for the external storage of the texture.
857 * @param texels The texel data of the texture being created.
858 * @param baseWidth The width of the texture's base mipmap level
859 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800860 */
Brian Salomond1eaf492017-05-18 10:02:08 -0400861static bool allocate_and_populate_uncompressed_texture(GrPixelConfig config,
cblume55f2d2d2016-02-26 13:20:48 -0800862 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800863 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800864 GrGLenum target,
865 GrGLenum internalFormat,
brianosman81a84852016-09-12 09:05:14 -0700866 GrGLenum internalFormatForTexStorage,
cblume55f2d2d2016-02-26 13:20:48 -0800867 GrGLenum externalFormat,
868 GrGLenum externalType,
869 const SkTArray<GrMipLevel>& texels,
bsalomone699d0c2016-03-09 06:25:15 -0800870 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800871 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800872
Brian Salomond1eaf492017-05-18 10:02:08 -0400873 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800874 // We can only use TexStorage if we know we will not later change the storage requirements.
875 // This means if we may later want to add mipmaps, we cannot use TexStorage.
876 // Right now, we cannot know if we will later add mipmaps or not.
877 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500878 // mipmaps or are using a format incompatible with MIP maps.
Brian Salomond1eaf492017-05-18 10:02:08 -0400879 useTexStorage &= texels.count() > 1 || GrPixelConfigIsSint(config);
cblume790d5132016-02-29 11:13:29 -0800880
881 if (useTexStorage) {
882 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800883 GL_ALLOC_CALL(&interface,
Brian Salomond1eaf492017-05-18 10:02:08 -0400884 TexStorage2D(target, SkTMax(texels.count(), 1), internalFormatForTexStorage,
885 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400886 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800887 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800888 return false;
cblume790d5132016-02-29 11:13:29 -0800889 } else {
890 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
891 const void* currentMipData = texels[currentMipLevel].fPixels;
892 if (currentMipData == nullptr) {
893 continue;
894 }
895 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400896 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
897 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800898
899 GR_GL_CALL(&interface,
900 TexSubImage2D(target,
901 currentMipLevel,
902 0, // left
903 0, // top
904 currentWidth,
905 currentHeight,
906 externalFormat, externalType,
907 currentMipData));
908 }
bsalomone699d0c2016-03-09 06:25:15 -0800909 return true;
cblume790d5132016-02-29 11:13:29 -0800910 }
911 } else {
bsalomone699d0c2016-03-09 06:25:15 -0800912 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -0800913 GL_ALLOC_CALL(&interface,
914 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800915 0,
cblume790d5132016-02-29 11:13:29 -0800916 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800917 baseWidth,
918 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800919 0, // border
920 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800921 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400922 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800923 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800924 return false;
925 }
926 } else {
927 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
928 int twoToTheMipLevel = 1 << currentMipLevel;
929 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
930 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
931 const void* currentMipData = texels[currentMipLevel].fPixels;
932 // Even if curremtMipData is nullptr, continue to call TexImage2D.
933 // This will allocate texture memory which we can later populate.
934 GL_ALLOC_CALL(&interface,
935 TexImage2D(target,
936 currentMipLevel,
937 internalFormat,
938 currentWidth,
939 currentHeight,
940 0, // border
941 externalFormat, externalType,
942 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400943 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800944 if (error != GR_GL_NO_ERROR) {
945 return false;
946 }
cblume790d5132016-02-29 11:13:29 -0800947 }
cblume55f2d2d2016-02-26 13:20:48 -0800948 }
949 }
bsalomone699d0c2016-03-09 06:25:15 -0800950 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800951}
952
953/**
954 * Creates storage space for the texture and fills it with texels.
955 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400956 * @param config Compressed pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800957 * @param desc The surface descriptor for the texture being created.
958 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800959 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -0800960 * @param internalFormat The data format used for the internal storage of the texture.
961 * @param texels The texel data of the texture being created.
cblume55f2d2d2016-02-26 13:20:48 -0800962 */
Brian Salomond1eaf492017-05-18 10:02:08 -0400963static bool allocate_and_populate_compressed_texture(GrPixelConfig config,
cblume55f2d2d2016-02-26 13:20:48 -0800964 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800965 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800966 GrGLenum target, GrGLenum internalFormat,
967 const SkTArray<GrMipLevel>& texels,
968 int baseWidth, int baseHeight) {
969 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800970
Brian Salomond1eaf492017-05-18 10:02:08 -0400971 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800972 // We can only use TexStorage if we know we will not later change the storage requirements.
973 // This means if we may later want to add mipmaps, we cannot use TexStorage.
974 // Right now, we cannot know if we will later add mipmaps or not.
975 // The only time we can use TexStorage is when we already have the
976 // mipmaps.
977 useTexStorage &= texels.count() > 1;
cblume55f2d2d2016-02-26 13:20:48 -0800978
cblume790d5132016-02-29 11:13:29 -0800979 if (useTexStorage) {
980 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800981 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -0800982 TexStorage2D(target,
983 texels.count(),
984 internalFormat,
985 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400986 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800987 if (error != GR_GL_NO_ERROR) {
988 return false;
cblume790d5132016-02-29 11:13:29 -0800989 } else {
990 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
991 const void* currentMipData = texels[currentMipLevel].fPixels;
992 if (currentMipData == nullptr) {
993 continue;
994 }
995
996 int twoToTheMipLevel = 1 << currentMipLevel;
997 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
998 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
999
1000 // Make sure that the width and height that we pass to OpenGL
1001 // is a multiple of the block size.
Brian Salomond1eaf492017-05-18 10:02:08 -04001002 size_t dataSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
cblume790d5132016-02-29 11:13:29 -08001003 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1004 currentMipLevel,
1005 0, // left
1006 0, // top
1007 currentWidth,
1008 currentHeight,
1009 internalFormat,
1010 SkToInt(dataSize),
1011 currentMipData));
1012 }
1013 }
1014 } else {
1015 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1016 int twoToTheMipLevel = 1 << currentMipLevel;
1017 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1018 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1019
1020 // Make sure that the width and height that we pass to OpenGL
1021 // is a multiple of the block size.
Brian Salomond1eaf492017-05-18 10:02:08 -04001022 size_t dataSize = GrCompressedFormatDataSize(config, baseWidth, baseHeight);
cblume790d5132016-02-29 11:13:29 -08001023
1024 GL_ALLOC_CALL(&interface,
1025 CompressedTexImage2D(target,
1026 currentMipLevel,
1027 internalFormat,
1028 currentWidth,
1029 currentHeight,
1030 0, // border
1031 SkToInt(dataSize),
1032 texels[currentMipLevel].fPixels));
1033
Brian Salomon0ec981b2017-05-15 13:48:50 -04001034 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001035 if (error != GR_GL_NO_ERROR) {
1036 return false;
1037 }
cblume55f2d2d2016-02-26 13:20:48 -08001038 }
1039 }
1040
1041 return true;
1042}
1043
1044/**
1045 * After a texture is created, any state which was altered during its creation
1046 * needs to be restored.
1047 *
1048 * @param interface The GL interface to use.
1049 * @param caps The capabilities of the GL device.
1050 * @param restoreGLRowLength Should the row length unpacking be restored?
1051 * @param glFlipY Did GL flip the texture vertically?
1052 */
1053static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1054 bool restoreGLRowLength, bool glFlipY) {
1055 if (restoreGLRowLength) {
1056 SkASSERT(caps.unpackRowLengthSupport());
1057 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1058 }
1059 if (glFlipY) {
1060 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1061 }
1062}
1063
Brian Salomond1eaf492017-05-18 10:02:08 -04001064bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1065 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1066 int left, int top, int width, int height, GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001067 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001068 // If we're uploading compressed data then we should be using uploadCompressedTexData
1069 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1070
Brian Salomond1eaf492017-05-18 10:02:08 -04001071 SkASSERT(this->caps()->isConfigTexturable(texConfig));
bsalomon5b30c6f2015-12-17 14:17:34 -08001072
cblume55f2d2d2016-02-26 13:20:48 -08001073 // texels is const.
1074 // But we may need to flip the texture vertically to prepare it.
1075 // Rather than flip in place and alter the incoming data,
1076 // we allocate a new buffer to flip into.
1077 // This means we need to make a non-const shallow copy of texels.
1078 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1079
1080 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1081 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -08001082 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001083 }
1084
1085 const GrGLInterface* interface = this->glInterface();
1086 const GrGLCaps& caps = this->glCaps();
1087
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001088 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001089
1090 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001091 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001092 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001093
cblume55f2d2d2016-02-26 13:20:48 -08001094 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1095 int twoToTheMipLevel = 1 << currentMipLevel;
1096 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1097 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1098
cblume55f2d2d2016-02-26 13:20:48 -08001099 if (currentHeight > SK_MaxS32 ||
1100 currentWidth > SK_MaxS32) {
1101 return false;
1102 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001103 if (!GrSurfacePriv::AdjustWritePixelParams(texWidth, texHeight, bpp, &left, &top,
1104 &currentWidth, &currentHeight,
1105 &texelsShallowCopy[currentMipLevel].fPixels,
1106 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
cblume55f2d2d2016-02-26 13:20:48 -08001107 return false;
1108 }
1109 if (currentWidth < 0 || currentHeight < 0) {
1110 return false;
1111 }
1112 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001113
bsalomon5b30c6f2015-12-17 14:17:34 -08001114 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001115 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001116 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001117 GrGLenum externalFormat;
1118 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001119 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1120 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001121 return false;
1122 }
brianosman81a84852016-09-12 09:05:14 -07001123 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001124 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001125
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001126 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001127 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001128 * because our srcData has extra bytes past each row. If so, we need
1129 * to trim those off here, since GL ES may not let us specify
1130 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001131 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001132 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001133 bool swFlipY = false;
1134 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001135
Brian Salomond1eaf492017-05-18 10:02:08 -04001136 if (kBottomLeft_GrSurfaceOrigin == texOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001137 if (caps.unpackFlipYSupport()) {
1138 glFlipY = true;
1139 } else {
1140 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001141 }
cblume55f2d2d2016-02-26 13:20:48 -08001142 }
1143
1144 // in case we need a temporary, trimmed copy of the src pixels
1145 SkAutoSMalloc<128 * 128> tempStorage;
1146
1147 // find the combined size of all the mip levels and the relative offset of
1148 // each into the collective buffer
1149 size_t combined_buffer_size = 0;
1150 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1151 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1152 int twoToTheMipLevel = 1 << currentMipLevel;
1153 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1154 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1155 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1156 individual_mip_offsets.push_back(combined_buffer_size);
1157 combined_buffer_size += trimmedSize;
1158 }
1159 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1160
1161 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001162 int twoToTheMipLevel = 1 << currentMipLevel;
1163 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1164 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1165 const size_t trimRowBytes = currentWidth * bpp;
1166
1167 /*
1168 * check whether to allocate a temporary buffer for flipping y or
1169 * because our srcData has extra bytes past each row. If so, we need
1170 * to trim those off here, since GL ES may not let us specify
1171 * GL_UNPACK_ROW_LENGTH.
1172 */
1173 restoreGLRowLength = false;
1174
1175 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
ericrk154349b2016-05-10 14:36:53 -07001176
1177 // TODO: This optimization should be enabled with or without mips.
1178 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1179 // mip level, before calling glTexImage2D.
1180 const bool usesMips = texelsShallowCopy.count() > 1;
1181 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001182 // can't use this for flipping, only non-neg values allowed. :(
1183 if (rowBytes != trimRowBytes) {
1184 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001185 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001186 restoreGLRowLength = true;
1187 }
jvanverth17aa0472016-01-05 10:41:27 -08001188 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001189 if (trimRowBytes != rowBytes || swFlipY) {
1190 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001191 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1192 if (swFlipY && currentHeight >= 1) {
1193 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001194 }
cblume55f2d2d2016-02-26 13:20:48 -08001195 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1196 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001197 memcpy(dst, src, trimRowBytes);
1198 if (swFlipY) {
1199 src -= rowBytes;
1200 } else {
1201 src += rowBytes;
1202 }
1203 dst += trimRowBytes;
1204 }
1205 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001206 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1207 individual_mip_offsets[currentMipLevel];
1208 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001209 }
jvanverth17aa0472016-01-05 10:41:27 -08001210 } else {
1211 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001212 }
bsalomone699d0c2016-03-09 06:25:15 -08001213 }
1214
1215 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001216 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001217 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001218 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001219 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001220 }
cblume55f2d2d2016-02-26 13:20:48 -08001221
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001222 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001223 if (kNewTexture_UploadType == uploadType) {
1224 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
1225 succeeded = allocate_and_populate_uncompressed_texture(
1226 texConfig, *interface, caps, target, internalFormat,
1227 internalFormatForTexStorage, externalFormat, externalType, texelsShallowCopy,
1228 width, height);
1229 } else {
1230 succeeded = false;
1231 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001232 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001233 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001234 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001235 }
cblume55f2d2d2016-02-26 13:20:48 -08001236 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1237 currentMipLevel++) {
1238 int twoToTheMipLevel = 1 << currentMipLevel;
1239 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1240 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001241
1242 GL_CALL(TexSubImage2D(target,
1243 currentMipLevel,
1244 left, top,
1245 currentWidth,
1246 currentHeight,
1247 externalFormat, externalType,
1248 texelsShallowCopy[currentMipLevel].fPixels));
1249 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001250 }
1251
cblume55f2d2d2016-02-26 13:20:48 -08001252 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1253
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001254 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001255}
1256
krajcevski145d48c2014-06-11 16:07:50 -07001257// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001258// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001259// create a CompressedTexData struct that takes a desc/ptr and figures out
1260// the proper upload semantics. Then users can construct this function how they
1261// see fit if they want to go against the "standard" way to do it.
Brian Salomond1eaf492017-05-18 10:02:08 -04001262bool GrGLGpu::uploadCompressedTexData(GrPixelConfig config, int texWidth, int texHeight,
1263 GrSurfaceOrigin texOrigin, GrGLenum target,
1264 const SkTArray<GrMipLevel>& texels, UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001265 int left, int top, int width, int height) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001266 SkASSERT(this->caps()->isConfigTexturable(config));
krajcevski9c0e6292014-06-02 07:38:14 -07001267
1268 // No support for software flip y, yet...
Brian Salomond1eaf492017-05-18 10:02:08 -04001269 SkASSERT(kBottomLeft_GrSurfaceOrigin != texOrigin);
krajcevski9c0e6292014-06-02 07:38:14 -07001270
cblume55f2d2d2016-02-26 13:20:48 -08001271 const GrGLInterface* interface = this->glInterface();
1272 const GrGLCaps& caps = this->glCaps();
1273
krajcevski145d48c2014-06-11 16:07:50 -07001274 if (-1 == width) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001275 width = texWidth;
krajcevski145d48c2014-06-11 16:07:50 -07001276 }
1277#ifdef SK_DEBUG
1278 else {
Brian Salomond1eaf492017-05-18 10:02:08 -04001279 SkASSERT(width <= texWidth);
krajcevski145d48c2014-06-11 16:07:50 -07001280 }
1281#endif
1282
1283 if (-1 == height) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001284 height = texHeight;
krajcevski145d48c2014-06-11 16:07:50 -07001285 }
1286#ifdef SK_DEBUG
1287 else {
Brian Salomond1eaf492017-05-18 10:02:08 -04001288 SkASSERT(height <= texHeight);
krajcevski145d48c2014-06-11 16:07:50 -07001289 }
1290#endif
1291
bsalomon76148af2016-01-12 11:13:47 -08001292 // We only need the internal format for compressed 2D textures.
1293 GrGLenum internalFormat;
Brian Salomond1eaf492017-05-18 10:02:08 -04001294 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001295 return false;
1296 }
krajcevski9c0e6292014-06-02 07:38:14 -07001297
jvanverth17aa0472016-01-05 10:41:27 -08001298 if (kNewTexture_UploadType == uploadType) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001299 return allocate_and_populate_compressed_texture(config, *interface, caps, target,
cblume790d5132016-02-29 11:13:29 -08001300 internalFormat, texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001301 } else {
cblume55f2d2d2016-02-26 13:20:48 -08001302 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001303 SkASSERT(texels[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001304
1305 int twoToTheMipLevel = 1 << currentMipLevel;
1306 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1307 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1308
1309 // Make sure that the width and height that we pass to OpenGL
1310 // is a multiple of the block size.
Brian Salomond1eaf492017-05-18 10:02:08 -04001311 size_t dataSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
cblume55f2d2d2016-02-26 13:20:48 -08001312 GL_CALL(CompressedTexSubImage2D(target,
1313 currentMipLevel,
1314 left, top,
1315 currentWidth,
1316 currentHeight,
1317 internalFormat,
1318 SkToInt(dataSize),
1319 texels[currentMipLevel].fPixels));
1320 }
krajcevski145d48c2014-06-11 16:07:50 -07001321 }
krajcevski9c0e6292014-06-02 07:38:14 -07001322
bsalomond4cb9222014-08-11 14:19:09 -07001323 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001324}
1325
bsalomon424cc262015-05-22 10:37:30 -07001326static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001327 int sampleCount,
1328 GrGLenum format,
1329 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001330 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001331 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001332 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001333 case GrGLCaps::kEXT_MSFBOType:
1334 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001335 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001336 GL_ALLOC_CALL(ctx.interface(),
1337 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1338 sampleCount,
1339 format,
1340 width, height));
1341 break;
1342 case GrGLCaps::kES_Apple_MSFBOType:
1343 GL_ALLOC_CALL(ctx.interface(),
1344 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1345 sampleCount,
1346 format,
1347 width, height));
1348 break;
1349 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1350 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1351 GL_ALLOC_CALL(ctx.interface(),
1352 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1353 sampleCount,
1354 format,
1355 width, height));
1356 break;
1357 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001358 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001359 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001360 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001361 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001362}
1363
egdanielb0e1be22015-04-22 13:27:39 -07001364bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001365 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001366 GrGLRenderTarget::IDDesc* idDesc) {
1367 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001368 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001369 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001370 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001371 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1372 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001373 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001374
1375 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001376
bsalomona11e5fc2015-12-18 07:59:41 -08001377 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001378
bsalomonb15b4c12014-10-29 12:41:57 -07001379 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001380 goto FAILED;
1381 }
1382
egdanield803f272015-03-18 13:01:52 -07001383 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1384 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001385 goto FAILED;
1386 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001387
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001388 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1389 // the texture bound to the other. The exception is the IMG multisample extension. With this
1390 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1391 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001392 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001393 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001394 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001395 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001396 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001397 goto FAILED;
1398 }
bsalomon76148af2016-01-12 11:13:47 -08001399 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1400 return false;
1401 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001402 } else {
egdanield803f272015-03-18 13:01:52 -07001403 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001404 }
1405
egdanield803f272015-03-18 13:01:52 -07001406 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001407 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001408 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001409 SkASSERT(desc.fSampleCnt > 0);
1410 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001411 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001412 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001413 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001414 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001415 goto FAILED;
1416 }
egdanield803f272015-03-18 13:01:52 -07001417 fStats.incRenderTargetBinds();
1418 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1419 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001420 GR_GL_COLOR_ATTACHMENT0,
1421 GR_GL_RENDERBUFFER,
1422 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001423 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001424 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1425 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1426 goto FAILED;
1427 }
bsalomon424cc262015-05-22 10:37:30 -07001428 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001429 }
1430 }
egdanield803f272015-03-18 13:01:52 -07001431 fStats.incRenderTargetBinds();
1432 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001433
bsalomonb15b4c12014-10-29 12:41:57 -07001434 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001435 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001436 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001437 texInfo.fTarget,
1438 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001439 } else {
egdanield803f272015-03-18 13:01:52 -07001440 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001441 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001442 texInfo.fTarget,
1443 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001444 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001445 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001446 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001447 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1448 goto FAILED;
1449 }
bsalomon424cc262015-05-22 10:37:30 -07001450 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001451 }
1452
1453 return true;
1454
1455FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001456 if (idDesc->fMSColorRenderbufferID) {
1457 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001458 }
egdanield803f272015-03-18 13:01:52 -07001459 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1460 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001461 }
egdanield803f272015-03-18 13:01:52 -07001462 if (idDesc->fTexFBOID) {
1463 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001464 }
1465 return false;
1466}
1467
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001468// good to set a break-point here to know when createTexture fails
1469static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001470// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001471 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001472}
1473
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001474#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001475static size_t as_size_t(int x) {
1476 return x;
1477}
1478#endif
1479
kkinnunen2e6055b2016-04-22 01:48:29 -07001480static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface) {
cblume55f2d2d2016-02-26 13:20:48 -08001481 GrGLTexture::IDDesc idDesc;
1482 idDesc.fInfo.fID = 0;
1483 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
kkinnunen2e6055b2016-04-22 01:48:29 -07001484 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
cblume55f2d2d2016-02-26 13:20:48 -08001485 // When we create the texture, we only
1486 // create GL_TEXTURE_2D at the moment.
1487 // External clients can do something different.
1488 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1489 return idDesc;
1490}
1491
1492static void set_initial_texture_params(const GrGLInterface* interface,
1493 const GrGLTextureInfo& info,
1494 GrGLTexture::TexParams* initialTexParams) {
1495 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1496 // drivers have a bug where an FBO won't be complete if it includes a
1497 // texture that is not mipmap complete (considering the filter in use).
1498 // we only set a subset here so invalidate first
1499 initialTexParams->invalidate();
1500 initialTexParams->fMinFilter = GR_GL_NEAREST;
1501 initialTexParams->fMagFilter = GR_GL_NEAREST;
1502 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1503 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1504 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1505 GR_GL_TEXTURE_MAG_FILTER,
1506 initialTexParams->fMagFilter));
1507 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1508 GR_GL_TEXTURE_MIN_FILTER,
1509 initialTexParams->fMinFilter));
1510 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1511 GR_GL_TEXTURE_WRAP_S,
1512 initialTexParams->fWrapS));
1513 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1514 GR_GL_TEXTURE_WRAP_T,
1515 initialTexParams->fWrapT));
1516}
1517
egdanielb0e1be22015-04-22 13:27:39 -07001518GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001519 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001520 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001521 // We fail if the MSAA was requested and is not available.
1522 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001523 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001524 return return_null_texture();
1525 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001526
Brian Salomond34edf32017-05-19 15:45:48 -04001527 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001528
bsalomonb15b4c12014-10-29 12:41:57 -07001529 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001530 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001531 GrGLTexture::TexParams initialTexParams;
Brian Salomond34edf32017-05-19 15:45:48 -04001532 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001533 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001534 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001535
Robert Phillipsd6214d42016-11-07 08:23:48 -05001536 bool wasMipMapDataProvided = false;
1537 if (texels.count() > 1) {
1538 wasMipMapDataProvided = true;
1539 }
1540
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001541 GrGLTexture* tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001542 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001543 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001544 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001545 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001546
kkinnunen2e6055b2016-04-22 01:48:29 -07001547 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001548 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001549 return return_null_texture();
1550 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001551 tex = new GrGLTextureRenderTarget(this, budgeted, desc, idDesc, rtIDDesc,
1552 wasMipMapDataProvided);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001553 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -07001554 tex = new GrGLTexture(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001556 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001557#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001558 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001559 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001560#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001561 return tex;
1562}
1563
egdanielb0e1be22015-04-22 13:27:39 -07001564GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001565 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001566 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001567 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001568 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001569 return return_null_texture();
1570 }
1571
kkinnunen2e6055b2016-04-22 01:48:29 -07001572 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface());
bsalomon091f60c2015-11-10 11:54:56 -08001573 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001574 return return_null_texture();
1575 }
1576
1577 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001578 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001579
krajcevski9c0e6292014-06-02 07:38:14 -07001580 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001581 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001582
Brian Salomond1eaf492017-05-18 10:02:08 -04001583 if (!this->uploadCompressedTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin,
1584 idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001585 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001586 return return_null_texture();
1587 }
1588
1589 GrGLTexture* tex;
kkinnunen2e6055b2016-04-22 01:48:29 -07001590 tex = new GrGLTexture(this, budgeted, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001591 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1592#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001593 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001594 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001595#endif
1596 return tex;
1597}
1598
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001599namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001600
egdaniel8dc7c3a2015-04-16 11:22:42 -07001601const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001602
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001603void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001604 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001605
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001606 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001607 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001608 (kUnknownBitCount == format->fTotalBits));
1609 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001610 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001611 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1612 (GrGLint*)&format->fStencilBits);
1613 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001614 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001615 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1616 (GrGLint*)&format->fTotalBits);
1617 format->fTotalBits += format->fStencilBits;
1618 } else {
1619 format->fTotalBits = format->fStencilBits;
1620 }
1621 }
1622}
1623}
1624
egdanielff1d5472015-09-10 08:37:20 -07001625int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001626 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001627 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001628 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1629 // Default to unsupported, set this if we find a stencil format that works.
1630 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001631 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001632 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001633 GL_CALL(GenTextures(1, &colorID));
1634 this->setScratchTextureUnit();
1635 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1636 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1637 GR_GL_TEXTURE_MAG_FILTER,
1638 GR_GL_NEAREST));
1639 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1640 GR_GL_TEXTURE_MIN_FILTER,
1641 GR_GL_NEAREST));
1642 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1643 GR_GL_TEXTURE_WRAP_S,
1644 GR_GL_CLAMP_TO_EDGE));
1645 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1646 GR_GL_TEXTURE_WRAP_T,
1647 GR_GL_CLAMP_TO_EDGE));
1648
bsalomon76148af2016-01-12 11:13:47 -08001649 GrGLenum internalFormat;
1650 GrGLenum externalFormat;
1651 GrGLenum externalType;
1652 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1653 &externalType)) {
1654 return false;
1655 }
egdanielff1d5472015-09-10 08:37:20 -07001656 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1657 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001658 0,
bsalomon76148af2016-01-12 11:13:47 -08001659 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001660 kSize,
1661 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001662 0,
bsalomon76148af2016-01-12 11:13:47 -08001663 externalFormat,
1664 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001665 NULL));
bsalomon30447372015-12-21 09:03:05 -08001666 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001667 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001668 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001669 }
1670
1671 // unbind the texture from the texture unit before binding it to the frame buffer
1672 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1673
1674 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001675 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001676 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001677 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001678 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001679 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1680 GR_GL_COLOR_ATTACHMENT0,
1681 GR_GL_TEXTURE_2D,
1682 colorID,
1683 0));
bsalomon30447372015-12-21 09:03:05 -08001684 GrGLuint sbRBID = 0;
1685 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001686
1687 // look over formats till I find a compatible one
1688 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001689 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001690 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001691 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1692 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1693 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1694 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1695 sFmt.fInternalFormat,
1696 kSize, kSize));
1697 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001698 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001699 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001700 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001701 if (sFmt.fPacked) {
1702 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1703 GR_GL_DEPTH_ATTACHMENT,
1704 GR_GL_RENDERBUFFER, sbRBID));
1705 } else {
1706 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1707 GR_GL_DEPTH_ATTACHMENT,
1708 GR_GL_RENDERBUFFER, 0));
1709 }
1710 GrGLenum status;
1711 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1712 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1713 firstWorkingStencilFormatIndex = i;
1714 break;
1715 }
egdanielff1d5472015-09-10 08:37:20 -07001716 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1717 GR_GL_STENCIL_ATTACHMENT,
1718 GR_GL_RENDERBUFFER, 0));
1719 if (sFmt.fPacked) {
1720 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1721 GR_GL_DEPTH_ATTACHMENT,
1722 GR_GL_RENDERBUFFER, 0));
1723 }
egdanielff1d5472015-09-10 08:37:20 -07001724 }
1725 }
bsalomon30447372015-12-21 09:03:05 -08001726 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001727 }
1728 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001729 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1730 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001731 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001732 }
bsalomon30447372015-12-21 09:03:05 -08001733 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001734}
1735
erikchen9a1ed5d2016-02-10 16:32:34 -08001736bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001737 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1738 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001739 info->fID = 0;
1740 info->fTarget = GR_GL_TEXTURE_2D;
1741 GL_CALL(GenTextures(1, &(info->fID)));
1742
1743 if (!info->fID) {
1744 return false;
1745 }
1746
1747 this->setScratchTextureUnit();
1748 GL_CALL(BindTexture(info->fTarget, info->fID));
1749
1750 if (renderTarget && this->glCaps().textureUsageSupport()) {
1751 // provides a hint about how this texture will be used
1752 GL_CALL(TexParameteri(info->fTarget,
1753 GR_GL_TEXTURE_USAGE,
1754 GR_GL_FRAMEBUFFER_ATTACHMENT));
1755 }
1756
cblume55f2d2d2016-02-26 13:20:48 -08001757 if (info) {
1758 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1759 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001760 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin, info->fTarget,
1761 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
1762 texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001763 GL_CALL(DeleteTextures(1, &(info->fID)));
1764 return false;
1765 }
1766 return true;
1767}
1768
egdanielec00d942015-09-14 12:56:10 -07001769GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001770 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001771 SkASSERT(width >= rt->width());
1772 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001773
vbuzinovdded6962015-06-12 08:59:45 -07001774 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001775 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001776
egdanielff1d5472015-09-10 08:37:20 -07001777 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001778 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001779 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001780 }
egdanielff1d5472015-09-10 08:37:20 -07001781
1782 if (!sbDesc.fRenderbufferID) {
1783 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1784 }
1785 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001786 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001787 }
1788 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1789 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1790 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1791 // we do this "if" so that we don't call the multisample
1792 // version on a GL that doesn't have an MSAA extension.
1793 if (samples > 0) {
1794 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1795 samples,
1796 sFmt.fInternalFormat,
1797 width, height));
1798 } else {
1799 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1800 sFmt.fInternalFormat,
1801 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001802 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001803 }
1804 fStats.incStencilAttachmentCreates();
1805 // After sized formats we attempt an unsized format and take
1806 // whatever sizes GL gives us. In that case we query for the size.
1807 GrGLStencilAttachment::Format format = sFmt;
1808 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001809 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1810 sbDesc,
1811 width,
1812 height,
1813 samples,
1814 format);
1815 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001816}
1817
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001818////////////////////////////////////////////////////////////////////////////////
1819
jvanverth73063dc2015-12-03 09:15:47 -08001820// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1821// objects are implemented as client-side-arrays on tile-deferred architectures.
1822#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1823
cdaltone2e71c22016-04-07 18:13:29 -07001824GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001825 GrAccessPattern accessPattern, const void* data) {
1826 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001827}
1828
Robert Phillipse3302df2017-04-24 07:31:02 -04001829
1830std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1831 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1832}
1833
csmartdaltone0d36292016-07-29 08:14:20 -07001834InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1835 return new GLInstancedRendering(this);
1836}
1837
bsalomon3e791242014-12-17 13:43:13 -08001838void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001839 const GrGLIRect& rtViewport,
1840 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001841 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001842 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001843 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001844 scissorState.rect().fLeft,
1845 scissorState.rect().fTop,
1846 scissorState.rect().width(),
1847 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001848 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001849 // if the scissor fully contains the viewport then we fall through and
1850 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001851 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001852 if (fHWScissorSettings.fRect != scissor) {
1853 scissor.pushToGLScissor(this->glInterface());
1854 fHWScissorSettings.fRect = scissor;
1855 }
1856 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1857 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1858 fHWScissorSettings.fEnabled = kYes_TriState;
1859 }
1860 return;
1861 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 }
joshualitt77b13072014-10-27 14:51:01 -07001863
1864 // See fall through note above
1865 this->disableScissor();
1866}
1867
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001868void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
1869 const GrGLRenderTarget* rt) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001870#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001871 typedef GrWindowRectsState::Mode Mode;
1872 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1873 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001874
1875 if (!this->caps()->maxWindowRectangles() ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001876 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001877 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001878 }
1879
csmartdalton7535f412016-08-23 06:51:00 -07001880 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1881 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001882 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1883 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001884
csmartdalton28341fa2016-08-17 10:00:21 -07001885 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001886 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001887 for (int i = 0; i < numWindows; ++i) {
Brian Salomon9a767722017-03-13 17:57:28 -04001888 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07001889 }
1890
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001891 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001892 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001893
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001894 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001895#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001896}
1897
1898void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001899#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001900 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001901 return;
1902 }
1903 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001904 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001905#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001906}
1907
ethannicholas28ef4452016-03-25 09:26:03 -07001908void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1909 if (fHWMinSampleShading != minSampleShading) {
1910 if (minSampleShading > 0.0) {
1911 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1912 GL_CALL(MinSampleShading(minSampleShading));
1913 }
1914 else {
1915 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1916 }
1917 fHWMinSampleShading = minSampleShading;
1918 }
1919}
1920
bsalomon2eda5b32016-09-21 10:53:24 -07001921bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1922 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001923 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001924 if (!program) {
1925 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1926 return false;
1927 }
1928
1929 program->generateMipmaps(primProc, pipeline);
1930
egdaniel080e6732014-12-22 07:35:52 -08001931 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001932 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001933
egdaniel080e6732014-12-22 07:35:52 -08001934 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001935 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001936
bsalomon6df86402015-06-01 10:41:49 -07001937 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001938 if (fHWProgramID != programID) {
1939 GL_CALL(UseProgram(programID));
1940 fHWProgramID = programID;
1941 }
1942
egdanield803f272015-03-18 13:01:52 -07001943 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001944 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001945 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07001946 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001947 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001948 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001949
cdalton74b8d322016-04-11 14:47:28 -07001950 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001951
brianosman898235c2016-04-06 07:38:23 -07001952 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001953 GrStencilSettings stencil;
1954 if (pipeline.isStencilEnabled()) {
1955 // TODO: attach stencil and create settings during render target flush.
1956 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1957 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1958 glRT->renderTargetPriv().numStencilBits());
1959 }
1960 this->flushStencil(stencil);
egdaniel8dd688b2015-01-22 10:16:09 -08001961 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001962 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
csmartdaltondabc91b2016-11-15 14:26:27 -07001963 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001964
1965 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001966 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001967 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001968
1969 return true;
1970}
1971
joshualitt873ad0e2015-01-20 09:08:51 -08001972void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001973 const GrBuffer* indexBuffer,
1974 const GrBuffer* vertexBuffer,
1975 int baseVertex) {
cdaltone2e71c22016-04-07 18:13:29 -07001976 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001977 if (indexBuffer) {
1978 SkASSERT(indexBuffer);
1979 SkASSERT(!indexBuffer->isMapped());
1980 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001981 } else {
1982 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001983 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001984
Chris Daltonafc23682017-05-05 17:19:45 +00001985 int vaCount = primProc.numAttribs();
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001986 attribState->enableVertexArrays(this, vaCount);
1987
joshualitt71c92602015-01-14 08:12:47 -08001988 if (vaCount > 0) {
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001989 SkASSERT(vertexBuffer);
1990 SkASSERT(!vertexBuffer->isMapped());
bsalomonbc3d0de2014-12-15 13:45:03 -08001991
joshualitt873ad0e2015-01-20 09:08:51 -08001992 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001993 size_t vertexBufferOffsetInBytes = stride * baseVertex + vertexBuffer->baseOffset();
1994 size_t attribOffset = 0;
bsalomonbc3d0de2014-12-15 13:45:03 -08001995
1996 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001997 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001998 attribState->set(this, attribIndex, vertexBuffer, attrib.fType, stride,
1999 vertexBufferOffsetInBytes + attribOffset);
2000 attribOffset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002001 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002002 }
2003}
2004
csmartdalton485a1202016-07-13 10:16:32 -07002005GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002006 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002007
cdaltone2e71c22016-04-07 18:13:29 -07002008 // Index buffer state is tied to the vertex array.
2009 if (kIndex_GrBufferType == type) {
2010 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002011 }
cdaltone2e71c22016-04-07 18:13:29 -07002012
2013 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2014 auto& bufferState = fHWBufferState[type];
2015
robertphillips8abb3702016-08-31 14:04:06 -07002016 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07002017 if (buffer->isCPUBacked()) {
2018 if (!bufferState.fBufferZeroKnownBound) {
2019 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
2020 }
2021 } else {
2022 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2023 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07002024 }
csmartdalton485a1202016-07-13 10:16:32 -07002025 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07002026 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002027 }
2028
2029 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002030}
2031
cdalton74b8d322016-04-11 14:47:28 -07002032void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
2033 if (buffer->hasAttachedToTexture()) {
2034 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05002035 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07002036 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
2037 auto& buffTex = fHWBufferTextures[i];
2038 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
2039 continue;
2040 }
2041 if (i == fHWMaxUsedBufferTextureUnit) {
2042 --fHWMaxUsedBufferTextureUnit;
2043 }
2044
2045 this->setTextureUnit(i);
2046 if (!buffTex.fKnownBound) {
2047 SkASSERT(buffTex.fTextureID);
2048 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
2049 buffTex.fKnownBound = true;
2050 }
2051 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
2052 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
2053 }
2054 }
2055}
2056
bsalomon861e1032014-12-16 07:33:49 -08002057void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002058 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002059 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002060 fHWScissorSettings.fEnabled = kNo_TriState;
2061 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002062 }
2063}
2064
csmartdalton29df7602016-08-31 11:55:52 -07002065void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07002066 this->handleDirtyContext();
2067
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002068 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002069 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002070 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002071
csmartdalton29df7602016-08-31 11:55:52 -07002072 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
2073 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002074 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00002075
2076 GrGLfloat r, g, b, a;
2077 static const GrGLfloat scale255 = 1.f / 255.f;
2078 a = GrColorUnpackA(color) * scale255;
2079 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002080 r = GrColorUnpackR(color) * scaleRGB;
2081 g = GrColorUnpackG(color) * scaleRGB;
2082 b = GrColorUnpackB(color) * scaleRGB;
2083
2084 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002085 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04002086
Eric Karlaeaf22b2017-05-18 15:08:09 -07002087 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2088 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Brian Salomon028a9a52017-05-11 11:39:08 -04002089#ifdef SK_BUILD_FOR_ANDROID
2090 // Android doesn't have std::nextafter but has nextafter.
Eric Karlaeaf22b2017-05-18 15:08:09 -07002091 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2092 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Brian Salomon028a9a52017-05-11 11:39:08 -04002093#else
Eric Karlaeaf22b2017-05-18 15:08:09 -07002094 static const GrGLfloat safeAlpha1 = std::nextafter(1.f, 2.f);
2095 static const GrGLfloat safeAlpha0 = std::nextafter(0.f, -1.f);
Brian Salomon028a9a52017-05-11 11:39:08 -04002096#endif
Eric Karlaeaf22b2017-05-18 15:08:09 -07002097 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002098 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002099 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002100 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002101}
2102
bsalomon861e1032014-12-16 07:33:49 -08002103void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002104 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002105 return;
2106 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002107 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002108 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002109
joshualitt77b13072014-10-27 14:51:01 -07002110 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002111 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002112
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002113 GL_CALL(StencilMask(0xffffffff));
2114 GL_CALL(ClearStencil(0));
2115 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002116 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002117}
2118
csmartdalton29df7602016-08-31 11:55:52 -07002119void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2120 bool insideStencilMask,
2121 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07002122 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002123 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002124
egdaniel8dc7c3a2015-04-16 11:22:42 -07002125 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002126 // this should only be called internally when we know we have a
2127 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002128 SkASSERT(sb);
2129 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002130#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002131 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002132 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002133#else
2134 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002135 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002136 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002137 // changing the clip between stencil passes may or may not
2138 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002140#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002141 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002142 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002143 value = (1 << (stencilBitCount - 1));
2144 } else {
2145 value = 0;
2146 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002147 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002148 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002149
csmartdalton29df7602016-08-31 11:55:52 -07002150 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002151 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002152
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002153 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002154 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002155 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002156 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002157}
2158
Brian Salomon71d9d842016-11-03 13:42:00 -04002159static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002160 int width, int height, GrPixelConfig config,
2161 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002162 // If the surface is already TopLeft, we don't need to flip.
2163 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002164 return false;
2165 }
2166
bsalomon494aa592015-07-23 11:45:02 -07002167 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002168 static const int kMinSize = 32;
2169 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002170 return false;
2171 }
2172
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002173 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002174 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002175 return false;
2176 }
2177
2178 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002179 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002180 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2181 // clipped.
2182 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2183}
2184
bsalomon1aa20292016-01-22 08:16:09 -08002185bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002186#ifdef SK_BUILD_FOR_MAC
2187 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2188 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2189 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2190 // we always use a temporary surface to test for read pixels support.
2191 // https://www.crbug.com/662802
2192 if (this->glContext().driver() == kChromium_GrGLDriver) {
2193 return this->readPixelsSupported(target->config(), readConfig);
2194 }
2195#endif
bsalomon1aa20292016-01-22 08:16:09 -08002196 auto bindRenderTarget = [this, target]() -> bool {
2197 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2198 return true;
2199 };
bsalomon2c3db322016-11-08 13:26:24 -08002200 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002201 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2202 GR_GL_GetIntegerv(this->glInterface(), query, value);
2203 };
2204 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002205 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2206 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002207}
2208
2209bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002210 sk_sp<GrTexture> temp;
2211 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
bsalomon1aa20292016-01-22 08:16:09 -08002212 GrTextureDesc desc;
2213 desc.fConfig = rtConfig;
2214 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002215 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2216 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2217 temp.reset(this->createTexture(desc, SkBudgeted::kNo));
2218 if (!temp) {
2219 return false;
2220 }
2221 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2222 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2223 return true;
2224 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
2225 temp.reset(this->createTexture(desc, SkBudgeted::kNo));
2226 if (!temp) {
2227 return false;
2228 }
2229 GrGLIRect vp;
2230 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002231 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002232 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002233 }
bsalomon2c3db322016-11-08 13:26:24 -08002234 return false;
2235 };
2236 auto unbindRenderTarget = [this, &temp]() {
2237 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002238 };
2239 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2240 GR_GL_GetIntegerv(this->glInterface(), query, value);
2241 };
bsalomon2c3db322016-11-08 13:26:24 -08002242 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2243 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002244}
2245
2246bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2247 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2248 return this->readPixelsSupported(rt, readConfig);
2249 } else {
2250 GrPixelConfig config = surfaceForConfig->config();
2251 return this->readPixelsSupported(config, readConfig);
2252 }
2253}
2254
bsalomone9573312016-01-25 14:33:25 -08002255static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2256 if (GrPixelConfigIsSRGB(a)) {
2257 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2258 } else if (GrPixelConfigIsSRGB(b)) {
2259 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2260 }
2261 return false;
2262}
2263
bsalomonf0674512015-07-28 13:26:15 -07002264bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2265 GrPixelConfig readConfig, DrawPreference* drawPreference,
2266 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002267 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002268
bsalomone9573312016-01-25 14:33:25 -08002269 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002270 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2271 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2272 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2273 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2274 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002275 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2276 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002277 // For now assume no swizzling, we may change that below.
2278 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2279
2280 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2281 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002282 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002283 // base class will fail for us.
2284 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2285 tempDrawInfo->fReadConfig = readConfig;
2286
2287 if (requires_srgb_conversion(srcConfig, readConfig)) {
2288 if (!this->readPixelsSupported(readConfig, readConfig)) {
2289 return false;
2290 }
2291 // Draw to do srgb to linear conversion or vice versa.
2292 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2293 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2294 tempDrawInfo->fReadConfig = readConfig;
2295 return true;
2296 }
2297
bsalomon1aa20292016-01-22 08:16:09 -08002298 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2299 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002300 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002301 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2302 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002303 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002304 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002305 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002306 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2307 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002308 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002309 // Better to do a draw with a R/B swap and then read as the original config.
2310 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002311 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2312 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002313 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002314 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2315 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002316 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002317 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002318 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2319 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2320 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002321 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2322 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2323 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2324 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002325 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002326 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002327 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2328 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2329 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2330 // will effectively be sBGRA).
2331 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2332 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2333 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2334 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002335 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2336 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2337 // it's unsupported, but 32bit RGBA reads are supported.
2338 // Don't attempt to do any srgb conversions since we only care about alpha.
2339 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2340 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2341 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2342 }
2343 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2344 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2345 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002346 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002347 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2348 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2349 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2350 } else {
2351 return false;
2352 }
2353 } else {
2354 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2355 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2356 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002357 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002358 this->readPixelsSupported(readConfig, readConfig)) {
2359 // Do a draw to convert from the src config to the read config.
2360 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2361 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2362 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002363 } else {
2364 return false;
2365 }
bsalomon39826022015-07-23 08:07:21 -07002366 }
2367
Brian Salomon71d9d842016-11-03 13:42:00 -04002368 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
2369 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
2370 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002371 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002372 }
2373
bsalomon39826022015-07-23 08:07:21 -07002374 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002375}
2376
bsalomon6cb3cbe2015-07-30 07:34:27 -07002377bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002378 int left, int top,
2379 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002380 GrPixelConfig config,
2381 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002382 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002383 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002384
bsalomone9573312016-01-25 14:33:25 -08002385 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002386 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002387 return false;
2388 }
2389
bsalomon16921ec2015-07-30 15:34:56 -07002390 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002391 if (requires_srgb_conversion(surface->config(), config)) {
2392 return false;
2393 }
2394
2395 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2396 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002397 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002398 // Don't attempt to do any srgb conversions since we only care about alpha.
2399 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002400 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002401 tempConfig = kSRGBA_8888_GrPixelConfig;
2402 }
2403 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002404 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002405 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Brian Salomon71d9d842016-11-03 13:42:00 -04002406 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
bsalomone9573312016-01-25 14:33:25 -08002407 width*4)) {
2408 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2409 for (int j = 0; j < height; ++j) {
2410 for (int i = 0; i < width; ++i) {
2411 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2412 }
2413 }
2414 return true;
2415 }
2416 }
bsalomon16921ec2015-07-30 15:34:56 -07002417 return false;
2418 }
2419
bsalomon76148af2016-01-12 11:13:47 -08002420 GrGLenum externalFormat;
2421 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002422 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002423 &externalType)) {
2424 return false;
2425 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002426 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002427
Brian Salomon71d9d842016-11-03 13:42:00 -04002428 GrGLIRect glvp;
2429 if (renderTarget) {
2430 // resolve the render target if necessary
2431 switch (renderTarget->getResolveType()) {
2432 case GrGLRenderTarget::kCantResolve_ResolveType:
2433 return false;
2434 case GrGLRenderTarget::kAutoResolves_ResolveType:
2435 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2436 break;
2437 case GrGLRenderTarget::kCanResolve_ResolveType:
2438 this->onResolveRenderTarget(renderTarget);
2439 // we don't track the state of the READ FBO ID.
2440 fStats.incRenderTargetBinds();
2441 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2442 break;
2443 default:
2444 SkFAIL("Unknown resolve type");
2445 }
2446 glvp = renderTarget->getViewport();
2447 } else {
2448 // Use a temporary FBO.
2449 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002450 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002451 }
2452
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002453 // the read rect is viewport-relative
2454 GrGLIRect readRect;
Brian Salomon71d9d842016-11-03 13:42:00 -04002455 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002456
bsalomon9d02b262016-02-01 12:49:30 -08002457 size_t bytesPerPixel = GrBytesPerPixel(config);
2458 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002459
bsalomon@google.comc6980972011-11-02 19:57:21 +00002460 size_t readDstRowBytes = tightRowBytes;
2461 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002462
bsalomon@google.comc6980972011-11-02 19:57:21 +00002463 // determine if GL can read using the passed rowBytes or if we need
2464 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002465 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002466 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002467 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002468 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002469 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002470 readDstRowBytes = rowBytes;
2471 } else {
2472 scratch.reset(tightRowBytes * height);
2473 readDst = scratch.get();
2474 }
2475 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002476 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002477 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2478 }
bsalomonf46a1242015-12-15 12:37:38 -08002479 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2480
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002481 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2482 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002483 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002484 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002485 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002486 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
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, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002490 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002491 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002492
2493 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002494 // API presents top-to-bottom. We must preserve the padding contents. Note
2495 // that the above readPixels did not overwrite the padding.
2496 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002497 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002498 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002499 scratch.reset(tightRowBytes);
2500 void* tmpRow = scratch.get();
2501 // flip y in-place by rows
2502 const int halfY = height >> 1;
2503 char* top = reinterpret_cast<char*>(buffer);
2504 char* bottom = top + (height - 1) * rowBytes;
2505 for (int y = 0; y < halfY; y++) {
2506 memcpy(tmpRow, top, tightRowBytes);
2507 memcpy(top, bottom, tightRowBytes);
2508 memcpy(bottom, tmpRow, tightRowBytes);
2509 top += rowBytes;
2510 bottom -= rowBytes;
2511 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002512 }
2513 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002514 SkASSERT(readDst != buffer);
2515 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002516 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002517 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002518 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002519 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002520 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002521 dst += (height-1) * rowBytes;
2522 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002523 for (int y = 0; y < height; y++) {
2524 memcpy(dst, src, tightRowBytes);
2525 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002526 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002527 dst += rowBytes;
2528 } else {
2529 dst -= rowBytes;
2530 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002531 }
2532 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002533 if (!renderTarget) {
2534 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2535 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002536 return true;
2537}
2538
egdaniel9cb63402016-06-23 08:37:05 -07002539GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -07002540 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2541 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -05002542 return new GrGLGpuCommandBuffer(this);
egdaniel066df7c2016-06-08 14:02:27 -07002543}
2544
brianosman64d094d2016-03-25 06:01:59 -07002545void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002546 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002547
Robert Phillips294870f2016-11-11 12:38:40 -05002548 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002549 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002550 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002551 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2552#ifdef SK_DEBUG
2553 // don't do this check in Chromium -- this is causing
2554 // lots of repeated command buffer flushes when the compositor is
2555 // rendering with Ganesh, which is really slow; even too slow for
2556 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002557 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002558 GrGLenum status;
2559 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2560 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2561 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2562 }
bsalomon160f24c2015-03-17 15:55:42 -07002563 }
egdanield803f272015-03-18 13:01:52 -07002564#endif
2565 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002566 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002567 }
2568
brianosman35b784d2016-05-05 11:52:53 -07002569 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002570 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002571 }
brianosman64d094d2016-03-25 06:01:59 -07002572
bsalomon083617b2016-02-12 12:10:14 -08002573 this->didWriteToSurface(target, bounds);
2574}
bsalomona9909122016-01-23 10:41:40 -08002575
brianosman33f6b3f2016-06-02 05:49:21 -07002576void GrGLGpu::flushFramebufferSRGB(bool enable) {
2577 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2578 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2579 fHWSRGBFramebuffer = kYes_TriState;
2580 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2581 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2582 fHWSRGBFramebuffer = kNo_TriState;
2583 }
2584}
2585
bsalomon083617b2016-02-12 12:10:14 -08002586void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2587 if (fHWViewport != viewport) {
2588 viewport.pushToGLViewport(this->glInterface());
2589 fHWViewport = viewport;
2590 }
2591}
2592
twiz@google.com0f31ca72011-03-18 17:38:11 +00002593GrGLenum gPrimitiveType2GLMode[] = {
2594 GR_GL_TRIANGLES,
2595 GR_GL_TRIANGLE_STRIP,
2596 GR_GL_TRIANGLE_FAN,
2597 GR_GL_POINTS,
2598 GR_GL_LINES,
2599 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002600};
2601
bsalomon@google.comd302f142011-03-03 13:54:13 +00002602#define SWAP_PER_DRAW 0
2603
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002604#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002605 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002606 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002607 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002608 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002609 void SwapBuf() {
2610 DWORD procID = GetCurrentProcessId();
2611 HWND hwnd = GetTopWindow(GetDesktopWindow());
2612 while(hwnd) {
2613 DWORD wndProcID = 0;
2614 GetWindowThreadProcessId(hwnd, &wndProcID);
2615 if(wndProcID == procID) {
2616 SwapBuffers(GetDC(hwnd));
2617 }
2618 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2619 }
2620 }
2621 #endif
2622#endif
2623
egdaniel9cb63402016-06-23 08:37:05 -07002624void GrGLGpu::draw(const GrPipeline& pipeline,
2625 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002626 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002627 int meshCount) {
2628 this->handleDirtyContext();
2629
bsalomon2eda5b32016-09-21 10:53:24 -07002630 bool hasPoints = false;
2631 for (int i = 0; i < meshCount; ++i) {
Chris Daltonbca46e22017-05-15 11:03:26 -06002632 if (meshes[i].primitiveType() == kPoints_GrPrimitiveType) {
bsalomon2eda5b32016-09-21 10:53:24 -07002633 hasPoints = true;
2634 break;
2635 }
2636 }
2637 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002638 return;
2639 }
ethannicholas22793252016-01-30 09:59:10 -08002640
egdaniel0e1853c2016-03-17 11:35:45 -07002641 for (int i = 0; i < meshCount; ++i) {
2642 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2643 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2644 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002645
egdaniel0e1853c2016-03-17 11:35:45 -07002646 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -06002647 const GrGLenum primType = gPrimitiveType2GLMode[mesh.primitiveType()];
2648
2649 if (mesh.isIndexed()) {
2650 GrGLvoid* indices = reinterpret_cast<void*>(mesh.indexBuffer()->baseOffset() +
2651 sizeof(uint16_t) * mesh.baseIndex());
2652 for (const GrMesh::PatternBatch batch : mesh) {
2653 this->setupGeometry(primProc, mesh.indexBuffer(), mesh.vertexBuffer(),
2654 batch.fBaseVertex);
Chris Dalton9926f4b2017-05-17 15:15:50 -06002655 // batch.fBaseVertex was accounted for by setupGeometry.
bsalomonfc9527a2016-08-29 09:18:39 -07002656 if (this->glCaps().drawRangeElementsSupport()) {
Brian Salomon09d994e2016-12-21 11:14:46 -05002657 // We assume here that the GrMeshDrawOps that generated the mesh used the full
bsalomonfc9527a2016-08-29 09:18:39 -07002658 // 0..vertexCount()-1 range.
2659 int start = 0;
Chris Daltonbca46e22017-05-15 11:03:26 -06002660 int end = mesh.vertexCount() * batch.fRepeatCount - 1;
2661 GL_CALL(DrawRangeElements(primType, start, end,
2662 mesh.indexCount() * batch.fRepeatCount,
2663 GR_GL_UNSIGNED_SHORT, indices));
bsalomonfc9527a2016-08-29 09:18:39 -07002664 } else {
Chris Daltonbca46e22017-05-15 11:03:26 -06002665 GL_CALL(DrawElements(primType, mesh.indexCount() * batch.fRepeatCount,
2666 GR_GL_UNSIGNED_SHORT, indices));
bsalomonfc9527a2016-08-29 09:18:39 -07002667 }
Chris Daltonbca46e22017-05-15 11:03:26 -06002668 fStats.incNumDraws();
egdaniel0e1853c2016-03-17 11:35:45 -07002669 }
Chris Daltonbca46e22017-05-15 11:03:26 -06002670 } else {
Chris Dalton9926f4b2017-05-17 15:15:50 -06002671 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
2672 this->setupGeometry(primProc, mesh.indexBuffer(), mesh.vertexBuffer(),
2673 mesh.baseVertex());
2674 GL_CALL(DrawArrays(primType, 0, mesh.vertexCount()));
2675 } else {
2676 this->setupGeometry(primProc, mesh.indexBuffer(), mesh.vertexBuffer(), 0);
2677 GL_CALL(DrawArrays(primType, mesh.baseVertex(), mesh.vertexCount()));
2678 }
egdaniel0e1853c2016-03-17 11:35:45 -07002679 fStats.incNumDraws();
Chris Daltonff926502017-05-03 14:36:54 -04002680 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002681 }
ethannicholas22793252016-01-30 09:59:10 -08002682
bsalomon@google.comd302f142011-03-03 13:54:13 +00002683#if SWAP_PER_DRAW
2684 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002685 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002686 aglSwapBuffers(aglGetCurrentContext());
2687 int set_a_break_pt_here = 9;
2688 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002689 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002690 SwapBuf();
2691 int set_a_break_pt_here = 9;
2692 SwapBuf();
2693 #endif
2694#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002695}
2696
bsalomon861e1032014-12-16 07:33:49 -08002697void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002698 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002699 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002700 // Some extensions automatically resolves the texture when it is read.
2701 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002702 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2703 fStats.incRenderTargetBinds();
2704 fStats.incRenderTargetBinds();
2705 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2706 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2707 // make sure we go through flushRenderTarget() since we've modified
2708 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002709 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002710 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002711 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002712
bsalomon@google.com347c3822013-05-01 20:10:01 +00002713 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002714 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002715 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002716 scissorState.set(dirtyRect);
2717 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002718 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002719 GL_CALL(ResolveMultisampleFramebuffer());
2720 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002721 int l, b, r, t;
2722 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2723 this->glCaps().blitFramebufferSupportFlags()) {
2724 l = 0;
2725 b = 0;
2726 r = target->width();
2727 t = target->height();
2728 } else {
2729 GrGLIRect rect;
2730 rect.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2731 dirtyRect.width(), dirtyRect.height(), target->origin());
2732 l = rect.fLeft;
2733 b = rect.fBottom;
2734 r = rect.fLeft + rect.fWidth;
2735 t = rect.fBottom + rect.fHeight;
2736 }
derekf8c8f71a2014-09-16 06:24:57 -07002737
2738 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002739 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002740 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002741 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002742 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002743 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002744 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002745 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002746 }
2747}
2748
bsalomon@google.com411dad02012-06-05 20:24:20 +00002749namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002750
bsalomon@google.com411dad02012-06-05 20:24:20 +00002751
2752GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002753 static const GrGLenum gTable[kGrStencilOpCount] = {
2754 GR_GL_KEEP, // kKeep
2755 GR_GL_ZERO, // kZero
2756 GR_GL_REPLACE, // kReplace
2757 GR_GL_INVERT, // kInvert
2758 GR_GL_INCR_WRAP, // kIncWrap
2759 GR_GL_DECR_WRAP, // kDecWrap
2760 GR_GL_INCR, // kIncClamp
2761 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002762 };
cdalton93a379b2016-05-11 13:58:08 -07002763 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2764 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2765 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2766 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2767 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2768 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2769 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2770 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2771 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2772 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002773}
2774
2775void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002776 const GrStencilSettings::Face& face,
2777 GrGLenum glFace) {
2778 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2779 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2780 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002781
cdalton93a379b2016-05-11 13:58:08 -07002782 GrGLint ref = face.fRef;
2783 GrGLint mask = face.fTestMask;
2784 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002785
2786 if (GR_GL_FRONT_AND_BACK == glFace) {
2787 // we call the combined func just in case separate stencil is not
2788 // supported.
2789 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2790 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002791 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002792 } else {
2793 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2794 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002795 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002796 }
2797}
2798}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002799
bsalomon3e791242014-12-17 13:43:13 -08002800void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002801 if (stencilSettings.isDisabled()) {
2802 this->disableStencil();
2803 } else if (fHWStencilSettings != stencilSettings) {
2804 if (kYes_TriState != fHWStencilTestEnabled) {
2805 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2806 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002807 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002808 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002809 set_gl_stencil(this->glInterface(),
2810 stencilSettings.front(),
2811 GR_GL_FRONT);
2812 set_gl_stencil(this->glInterface(),
2813 stencilSettings.back(),
2814 GR_GL_BACK);
2815 } else {
2816 set_gl_stencil(this->glInterface(),
2817 stencilSettings.front(),
2818 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002819 }
joshualitta58fe352014-10-27 08:39:00 -07002820 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002821 }
2822}
2823
csmartdaltonc7d85332016-10-26 10:13:46 -07002824void GrGLGpu::disableStencil() {
2825 if (kNo_TriState != fHWStencilTestEnabled) {
2826 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2827 fHWStencilTestEnabled = kNo_TriState;
2828 fHWStencilSettings.invalidate();
2829 }
2830}
2831
cdaltonaf8bc7d2016-02-05 09:35:20 -08002832void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002833 // rt is only optional if useHWAA is false.
2834 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002835 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002836
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002837 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002838 if (useHWAA) {
2839 if (kYes_TriState != fMSAAEnabled) {
2840 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2841 fMSAAEnabled = kYes_TriState;
2842 }
2843 } else {
2844 if (kNo_TriState != fMSAAEnabled) {
2845 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2846 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002847 }
2848 }
2849 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002850
2851 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002852 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002853 // Since stencil is disabled and we want more samples than are in the color buffer, we
2854 // need to tell the rasterizer explicitly how many to run.
2855 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2856 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2857 fHWRasterMultisampleEnabled = kYes_TriState;
2858 }
2859 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2860 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2861 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2862 fHWNumRasterSamples = rt->numStencilSamples();
2863 }
2864 } else {
2865 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2866 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2867 fHWRasterMultisampleEnabled = kNo_TriState;
2868 }
2869 }
2870 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002871 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002872 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002873}
2874
bsalomon7f9b2e42016-01-12 13:29:26 -08002875void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002876 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002877 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002878
cdalton8917d622015-05-06 13:40:21 -07002879 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002880 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2881 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002882 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2883 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002884 if (blendOff) {
2885 if (kNo_TriState != fHWBlendState.fEnabled) {
2886 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002887
2888 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2889 // https://code.google.com/p/skia/issues/detail?id=3943
2890 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2891 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2892 SkASSERT(this->caps()->advancedBlendEquationSupport());
2893 // Set to any basic blending equation.
2894 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2895 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2896 fHWBlendState.fEquation = blend_equation;
2897 }
2898
egdanielb414f252014-07-29 13:15:47 -07002899 fHWBlendState.fEnabled = kNo_TriState;
2900 }
cdalton8917d622015-05-06 13:40:21 -07002901 return;
2902 }
2903
2904 if (kYes_TriState != fHWBlendState.fEnabled) {
2905 GL_CALL(Enable(GR_GL_BLEND));
2906 fHWBlendState.fEnabled = kYes_TriState;
2907 }
2908
2909 if (fHWBlendState.fEquation != equation) {
2910 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2911 fHWBlendState.fEquation = equation;
2912 }
2913
2914 if (GrBlendEquationIsAdvanced(equation)) {
2915 SkASSERT(this->caps()->advancedBlendEquationSupport());
2916 // Advanced equations have no other blend state.
2917 return;
2918 }
2919
bsalomone63ffef2016-02-05 07:17:34 -08002920 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002921 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2922 gXfermodeCoeff2Blend[dstCoeff]));
2923 fHWBlendState.fSrcCoeff = srcCoeff;
2924 fHWBlendState.fDstCoeff = dstCoeff;
2925 }
2926
bsalomon7f9b2e42016-01-12 13:29:26 -08002927 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2928 GrColor blendConst = blendInfo.fBlendConstant;
2929 blendConst = swizzle.applyTo(blendConst);
2930 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2931 GrGLfloat c[4];
2932 GrColorToRGBAFloat(blendConst, c);
2933 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2934 fHWBlendState.fConstColor = blendConst;
2935 fHWBlendState.fConstColorValid = true;
2936 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002937 }
2938}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002939
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002940static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002941 static const GrGLenum gWrapModes[] = {
2942 GR_GL_CLAMP_TO_EDGE,
2943 GR_GL_REPEAT,
2944 GR_GL_MIRRORED_REPEAT
2945 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002946 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002947 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2948 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2949 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2950 return gWrapModes[tm];
2951}
2952
egdanielb7e7d572015-11-04 04:23:53 -08002953static GrGLenum get_component_enum_from_char(char component) {
2954 switch (component) {
2955 case 'r':
2956 return GR_GL_RED;
2957 case 'g':
2958 return GR_GL_GREEN;
2959 case 'b':
2960 return GR_GL_BLUE;
2961 case 'a':
2962 return GR_GL_ALPHA;
2963 default:
2964 SkFAIL("Unsupported component");
2965 return 0;
2966 }
2967}
2968
2969/** If texture swizzling is available using tex parameters then it is preferred over mangling
2970 the generated shader code. This potentially allows greater reuse of cached shaders. */
2971static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002972 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002973 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002974 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002975 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002976 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002977 }
2978}
2979
Brian Salomon514baff2016-11-17 15:17:07 -05002980void GrGLGpu::bindTexture(int unitIdx, const GrSamplerParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -07002981 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002982 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002983
reed856e9d92015-09-30 12:21:45 -07002984#ifdef SK_DEBUG
2985 if (!this->caps()->npotTextureTileSupport()) {
2986 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2987 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2988 if (tileX || tileY) {
2989 const int w = texture->width();
2990 const int h = texture->height();
2991 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2992 }
2993 }
2994#endif
2995
bsalomon@google.comb8670992012-07-25 21:27:09 +00002996 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2997 // 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 +00002998 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002999 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003000 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003001 this->onResolveRenderTarget(texRT);
3002 }
3003
Robert Phillips294870f2016-11-11 12:38:40 -05003004 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003005 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003006 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003007 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003008 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003009 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003010 }
3011
bsalomon@google.com4c883782012-06-04 19:05:11 +00003012 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003013 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003014 bool setAll = timestamp < this->getResetTimestamp();
3015 GrGLTexture::TexParams newTexParams;
3016
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003017 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003018 GR_GL_NEAREST,
3019 GR_GL_LINEAR,
3020 GR_GL_LINEAR_MIPMAP_LINEAR
3021 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003022 static GrGLenum glMagFilterModes[] = {
3023 GR_GL_NEAREST,
3024 GR_GL_LINEAR,
3025 GR_GL_LINEAR
3026 };
Brian Salomon514baff2016-11-17 15:17:07 -05003027 GrSamplerParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003028
Brian Salomon514baff2016-11-17 15:17:07 -05003029 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
bsalomonefd7d452014-10-23 14:17:46 -07003030 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
Brian Salomon514baff2016-11-17 15:17:07 -05003031 filterMode = GrSamplerParams::kBilerp_FilterMode;
bsalomonefd7d452014-10-23 14:17:46 -07003032 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003033 }
bsalomonefd7d452014-10-23 14:17:46 -07003034
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003035 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3036 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003037
brianosman20471892016-12-02 06:43:32 -08003038 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003039 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003040 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003041 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003042 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003043 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003044 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003045
brianosman33f6b3f2016-06-02 05:49:21 -07003046#ifdef SK_DEBUG
3047 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon514baff2016-11-17 15:17:07 -05003048 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003049 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3050 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003051 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3052 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3053 : SkDestinationSurfaceColorMode::kLegacy;
3054 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003055 }
3056 }
3057#endif
3058
cblume55f2d2d2016-02-26 13:20:48 -08003059 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3060
bsalomon@google.comb8670992012-07-25 21:27:09 +00003061 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3062 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003063 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003064 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003065 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003066 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003067 }
3068 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3069 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003070 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003071 }
cblume55f2d2d2016-02-26 13:20:48 -08003072 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003073 // These are not supported in ES2 contexts
3074 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3075 if (newTexParams.fMaxMipMapLevel != 0) {
3076 this->setTextureUnit(unitIdx);
3077 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3078 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3079 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3080 newTexParams.fMaxMipMapLevel));
3081 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3082 newTexParams.fMaxMipMapLevel));
3083 }
cblume55f2d2d2016-02-26 13:20:48 -08003084 }
3085 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003086 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003087 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003088 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003089 }
3090 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003091 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003092 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003093 }
bsalomoncdee0092016-01-08 13:20:12 -08003094 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003095 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3096 oldTexParams.fSwizzleRGBA,
3097 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003098 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003099 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003100 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003101}
3102
csmartdalton1897cfd2016-06-03 08:50:54 -07003103void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003104 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3105 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003106
3107 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3108
3109 if (!buffTex.fKnownBound) {
3110 if (!buffTex.fTextureID) {
3111 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3112 if (!buffTex.fTextureID) {
3113 return;
3114 }
3115 }
3116
3117 this->setTextureUnit(unitIdx);
3118 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3119
3120 buffTex.fKnownBound = true;
3121 }
3122
robertphillips8abb3702016-08-31 14:04:06 -07003123 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003124 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003125
3126 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003127 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3128 this->glCaps().configSizedInternalFormat(texelConfig),
3129 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003130
cdalton74b8d322016-04-11 14:47:28 -07003131 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003132 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003133
3134 if (this->glCaps().textureSwizzleSupport() &&
3135 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3136 GrGLenum glSwizzle[4];
3137 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3138 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3139 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3140 }
3141
3142 buffer->setHasAttachedToTexture();
3143 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3144 }
3145}
3146
Brian Salomonf9f45122016-11-29 11:59:17 -05003147void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3148 SkASSERT(texture);
3149 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3150 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003151 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003152 switch (ioType) {
3153 case kRead_GrIOType:
3154 access = GR_GL_READ_ONLY;
3155 break;
3156 case kWrite_GrIOType:
3157 access = GR_GL_WRITE_ONLY;
3158 break;
3159 case kRW_GrIOType:
3160 access = GR_GL_READ_WRITE;
3161 break;
3162 }
3163 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3164 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3165 }
3166}
3167
Brian Salomon514baff2016-11-17 15:17:07 -05003168void GrGLGpu::generateMipmaps(const GrSamplerParams& params, bool allowSRGBInputs,
brianosman33f6b3f2016-06-02 05:49:21 -07003169 GrGLTexture* texture) {
3170 SkASSERT(texture);
3171
3172 // First, figure out if we need mips for this texture at all:
Brian Salomon514baff2016-11-17 15:17:07 -05003173 GrSamplerParams::FilterMode filterMode = params.filterMode();
brianosman33f6b3f2016-06-02 05:49:21 -07003174
Brian Salomon514baff2016-11-17 15:17:07 -05003175 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003176 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
Brian Salomon514baff2016-11-17 15:17:07 -05003177 filterMode = GrSamplerParams::kBilerp_FilterMode;
brianosman33f6b3f2016-06-02 05:49:21 -07003178 }
3179 }
3180
Brian Salomon514baff2016-11-17 15:17:07 -05003181 if (GrSamplerParams::kMipMap_FilterMode != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003182 return;
3183 }
3184
3185 // If this is an sRGB texture and the mips were previously built the "other" way
3186 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3187 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003188 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3189 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3190 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003191 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003192 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003193 texture->texturePriv().dirtyMipMaps(true);
3194 }
3195
3196 // If the mips aren't dirty, we're done:
3197 if (!texture->texturePriv().mipMapsAreDirty()) {
3198 return;
3199 }
3200
3201 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3202 // from the rt it will still be the last bound texture, but it needs resolving.
3203 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3204 if (texRT) {
3205 this->onResolveRenderTarget(texRT);
3206 }
3207
3208 GrGLenum target = texture->target();
3209 this->setScratchTextureUnit();
3210 GL_CALL(BindTexture(target, texture->textureID()));
3211
3212 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3213 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003214 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003215 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3216 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3217 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3218 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3219 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3220 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3221 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3222 srgbDecode = GR_GL_DECODE_EXT;
3223 }
3224 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003225 }
3226
3227 // Either do manual mipmap generation or (if that fails), just rely on the driver:
3228 if (!this->generateMipmap(texture, allowSRGBInputs)) {
3229 GL_CALL(GenerateMipmap(target));
3230 }
3231
brianosmanfe199872016-06-13 07:59:48 -07003232 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003233 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3234 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003235 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003236
3237 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3238 texture->textureParamsModified();
3239}
3240
cdalton74b8d322016-04-11 14:47:28 -07003241void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3242 this->setTextureUnit(unitIdx);
3243 if (this->glStandard() == kGLES_GrGLStandard) {
3244 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3245 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3246 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3247 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3248 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3249 } else {
3250 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3251 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3252 reinterpret_cast<const GrGLint*>(swizzle)));
3253 }
3254}
3255
egdaniel080e6732014-12-22 07:35:52 -08003256void GrGLGpu::flushColorWrite(bool writeColor) {
3257 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003258 if (kNo_TriState != fHWWriteToColor) {
3259 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3260 GR_GL_FALSE, GR_GL_FALSE));
3261 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003262 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003263 } else {
3264 if (kYes_TriState != fHWWriteToColor) {
3265 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3266 fHWWriteToColor = kYes_TriState;
3267 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003268 }
bsalomon3e791242014-12-17 13:43:13 -08003269}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003270
bsalomon861e1032014-12-16 07:33:49 -08003271void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003272 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003273 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003274 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003275 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003276 }
3277}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003278
bsalomon861e1032014-12-16 07:33:49 -08003279void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003280 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003281 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003282 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3283 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3284 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003285 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003286 // clear out the this field so that if a program does use this unit it will rebind the correct
3287 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003288 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003289}
3290
Brian Salomone5e7eb12016-10-14 16:18:33 -04003291// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
3292static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3293 const GrSurface* src,
3294 const SkIRect& srcRect,
3295 const SkIPoint& dstPoint,
3296 const GrGLGpu* gpu) {
3297 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003298 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3299 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003300 return false;
3301 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003302 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3303 // have filtered such cases out.
3304 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003305 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3306 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3307 const GrRenderTarget* dstRT = dst->asRenderTarget();
3308 const GrRenderTarget* srcRT = src->asRenderTarget();
3309 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3310 return false;
3311 }
3312 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3313 return false;
3314 }
3315 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3316 return false;
3317 }
3318 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3319 // We would mirror to compensate for origin changes. Note that copySurface is
3320 // specified such that the src and dst rects are the same.
3321 if (dst->origin() != src->origin()) {
3322 return false;
3323 }
3324 }
3325 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003326 if (srcRT && srcRT->numColorSamples()) {
3327 if (dstRT && !dstRT->numColorSamples()) {
3328 return false;
3329 }
3330 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3331 return false;
3332 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003333 }
3334 }
3335 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3336 if (dstRT && dstRT->numColorSamples() > 0) {
3337 return false;
3338 }
3339 }
3340 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3341 if (dst->config() != src->config()) {
3342 return false;
3343 }
3344 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3345 const GrRenderTarget* srcRT = src->asRenderTarget();
3346 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3347 return false;
3348 }
3349 }
3350 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003351 if (srcRT && srcRT->numColorSamples()) {
3352 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3353 return false;
3354 }
3355 if (dst->origin() != src->origin()) {
3356 return false;
3357 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003358 }
3359 }
3360 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003361}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003362
bsalomon7ea33f52015-11-22 14:51:00 -08003363static inline bool can_copy_texsubimage(const GrSurface* dst,
3364 const GrSurface* src,
3365 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003366 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3367 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3368 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003369 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003370 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3371 return false;
3372 }
3373 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3374 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3375 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003376 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003377 return false;
3378 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003379 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3380 // If the src is multisampled (and uses an extension where there is a separate MSAA
3381 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003382 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003383 return false;
3384 }
bsalomon7ea33f52015-11-22 14:51:00 -08003385
3386 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3387 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3388 // texture.
3389 if (!dstTex) {
3390 return false;
3391 }
3392
3393 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003394
bsalomon7ea33f52015-11-22 14:51:00 -08003395 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3396 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003397 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003398 !GrPixelConfigIsCompressed(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003399 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
bsalomon7ea33f52015-11-22 14:51:00 -08003400 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003401 return true;
3402 } else {
3403 return false;
3404 }
3405}
3406
3407// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3408// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003409void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3410 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003411 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003412 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003413 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003414 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003415 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003416 GrGLuint* tempFBOID;
3417 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003418
egdanield803f272015-03-18 13:01:52 -07003419 if (0 == *tempFBOID) {
3420 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003421 }
3422
egdanield803f272015-03-18 13:01:52 -07003423 fStats.incRenderTargetBinds();
3424 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3425 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003426 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003427 target,
robertphillips754f4e92014-09-18 13:52:08 -07003428 texID,
3429 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003430 viewport->fLeft = 0;
3431 viewport->fBottom = 0;
3432 viewport->fWidth = surface->width();
3433 viewport->fHeight = surface->height();
3434 } else {
egdanield803f272015-03-18 13:01:52 -07003435 fStats.incRenderTargetBinds();
3436 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003437 *viewport = rt->getViewport();
3438 }
egdaniel0f5f9672015-02-03 11:10:51 -08003439}
3440
Brian Salomon71d9d842016-11-03 13:42:00 -04003441void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3442 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003443 if (!surface->asRenderTarget()) {
3444 SkASSERT(surface->asTexture());
3445 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3446 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3447 GR_GL_COLOR_ATTACHMENT0,
3448 textureTarget,
3449 0,
3450 0));
3451 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003452}
3453
joshualitt1cbdcde2015-08-21 11:53:29 -07003454bool GrGLGpu::onCopySurface(GrSurface* dst,
3455 GrSurface* src,
3456 const SkIRect& srcRect,
3457 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003458 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3459 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003460 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3461 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003462 return false;
3463 }
bsalomon083617b2016-02-12 12:10:14 -08003464 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3465 bool preferCopy = SkToBool(dst->asRenderTarget());
3466 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003467 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3468 return true;
3469 }
bsalomon5df6fee2015-05-18 06:26:15 -07003470 }
cblume61214052016-01-26 09:10:48 -08003471
bsalomon6df86402015-06-01 10:41:49 -07003472 if (can_copy_texsubimage(dst, src, this)) {
3473 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3474 return true;
3475 }
3476
Brian Salomone5e7eb12016-10-14 16:18:33 -04003477 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003478 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3479 }
3480
bsalomon083617b2016-02-12 12:10:14 -08003481 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003482 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3483 return true;
3484 }
bsalomon083617b2016-02-12 12:10:14 -08003485 }
3486
bsalomon6df86402015-06-01 10:41:49 -07003487 return false;
3488}
3489
Brian Salomonbf7b6202016-11-11 16:08:03 -05003490bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
3491 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003492 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003493 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003494
3495 if (!fCopyProgramArrayBuffer) {
3496 static const GrGLfloat vdata[] = {
3497 0, 0,
3498 0, 1,
3499 1, 0,
3500 1, 1
3501 };
3502 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3503 kStatic_GrAccessPattern, vdata));
3504 }
3505 if (!fCopyProgramArrayBuffer) {
3506 return false;
3507 }
3508
3509 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3510 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3511 if (!fCopyPrograms[progIdx].fProgram) {
3512 return false;
3513 }
3514
Brian Salomon1edc5b92016-11-29 13:43:46 -05003515 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003516 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3517 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3518 GrShaderVar::kUniform_TypeModifier);
3519 GrShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3520 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3521 GrShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier);
3522 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003523
3524 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003525 if (shaderCaps->noperspectiveInterpolationSupport()) {
3526 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003527 vshaderTxt.appendf("#extension %s : require\n", extension);
3528 }
3529 vTexCoord.addModifier("noperspective");
3530 }
3531
Brian Salomon1edc5b92016-11-29 13:43:46 -05003532 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003533 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003534 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003535 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003536 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003537 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003538 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003539 vshaderTxt.append(";");
3540
3541 vshaderTxt.append(
3542 "// Copy Program VS\n"
3543 "void main() {"
3544 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3545 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3546 " gl_Position.zw = vec2(0, 1);"
3547 "}"
3548 );
3549
3550 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003551 if (shaderCaps->noperspectiveInterpolationSupport()) {
3552 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003553 fshaderTxt.appendf("#extension %s : require\n", extension);
3554 }
3555 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003556 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003557 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003558 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003559 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003560 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
cdaltone2e71c22016-04-07 18:13:29 -07003561 &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -05003562 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003563 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003564 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003565 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003566 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003567 fshaderTxt.appendf(
3568 "// Copy Program FS\n"
3569 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003570 " sk_FragColor = texture(u_texture, v_texCoord);"
3571 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003572 );
3573
3574 const char* str;
3575 GrGLint length;
3576
3577 str = vshaderTxt.c_str();
3578 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003579 SkSL::Program::Settings settings;
3580 settings.fCaps = shaderCaps;
3581 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003582 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3583 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003584 &fStats, settings, &inputs);
3585 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003586
3587 str = fshaderTxt.c_str();
3588 length = SkToInt(fshaderTxt.size());
3589 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3590 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003591 &fStats, settings, &inputs);
3592 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003593
3594 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3595
3596 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3597 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3598 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3599 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3600 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3601 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3602
3603 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3604
3605 GL_CALL(DeleteShader(vshader));
3606 GL_CALL(DeleteShader(fshader));
3607
3608 return true;
bsalomon6df86402015-06-01 10:41:49 -07003609}
3610
brianosman33f6b3f2016-06-02 05:49:21 -07003611bool GrGLGpu::createMipmapProgram(int progIdx) {
3612 const bool oddWidth = SkToBool(progIdx & 0x2);
3613 const bool oddHeight = SkToBool(progIdx & 0x1);
3614 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3615
Brian Salomon1edc5b92016-11-29 13:43:46 -05003616 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003617
3618 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3619 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3620 if (!fMipmapPrograms[progIdx].fProgram) {
3621 return false;
3622 }
3623
Brian Salomon1edc5b92016-11-29 13:43:46 -05003624 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003625 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3626 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3627 GrShaderVar::kUniform_TypeModifier);
3628 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3629 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003630 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003631 GrShaderVar vTexCoords[] = {
3632 GrShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3633 GrShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3634 GrShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3635 GrShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003636 };
Brian Salomon99938a82016-11-21 13:41:08 -05003637 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003638
3639 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003640 if (shaderCaps->noperspectiveInterpolationSupport()) {
3641 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003642 vshaderTxt.appendf("#extension %s : require\n", extension);
3643 }
3644 vTexCoords[0].addModifier("noperspective");
3645 vTexCoords[1].addModifier("noperspective");
3646 vTexCoords[2].addModifier("noperspective");
3647 vTexCoords[3].addModifier("noperspective");
3648 }
3649
Brian Salomon1edc5b92016-11-29 13:43:46 -05003650 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003651 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003652 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003653 vshaderTxt.append(";");
3654 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003655 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003656 vshaderTxt.append(";");
3657 }
3658
3659 vshaderTxt.append(
3660 "// Mipmap Program VS\n"
3661 "void main() {"
3662 " gl_Position.xy = a_vertex * vec2(2, 2) - vec2(1, 1);"
3663 " gl_Position.zw = vec2(0, 1);"
3664 );
3665
3666 // Insert texture coordinate computation:
3667 if (oddWidth && oddHeight) {
3668 vshaderTxt.append(
3669 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
3670 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + vec2(u_texCoordXform.x, 0);"
3671 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + vec2(0, u_texCoordXform.z);"
3672 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3673 );
3674 } else if (oddWidth) {
3675 vshaderTxt.append(
3676 " v_texCoord0 = a_vertex.xy * vec2(u_texCoordXform.y, 1);"
3677 " v_texCoord1 = a_vertex.xy * vec2(u_texCoordXform.y, 1) + vec2(u_texCoordXform.x, 0);"
3678 );
3679 } else if (oddHeight) {
3680 vshaderTxt.append(
3681 " v_texCoord0 = a_vertex.xy * vec2(1, u_texCoordXform.w);"
3682 " v_texCoord1 = a_vertex.xy * vec2(1, u_texCoordXform.w) + vec2(0, u_texCoordXform.z);"
3683 );
3684 } else {
3685 vshaderTxt.append(
3686 " v_texCoord0 = a_vertex.xy;"
3687 );
3688 }
3689
3690 vshaderTxt.append("}");
3691
3692 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003693 if (shaderCaps->noperspectiveInterpolationSupport()) {
3694 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003695 fshaderTxt.appendf("#extension %s : require\n", extension);
3696 }
3697 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003698 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
brianosman33f6b3f2016-06-02 05:49:21 -07003699 &fshaderTxt);
3700 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003701 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003702 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003703 fshaderTxt.append(";");
3704 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003705 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003706 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003707 fshaderTxt.append(
3708 "// Mipmap Program FS\n"
3709 "void main() {"
3710 );
3711
3712 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003713 fshaderTxt.append(
3714 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3715 " texture(u_texture, v_texCoord1) + "
3716 " texture(u_texture, v_texCoord2) + "
3717 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003718 );
3719 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003720 fshaderTxt.append(
3721 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3722 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003723 );
3724 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003725 fshaderTxt.append(
3726 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003727 );
3728 }
3729
3730 fshaderTxt.append("}");
3731
3732 const char* str;
3733 GrGLint length;
3734
3735 str = vshaderTxt.c_str();
3736 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003737 SkSL::Program::Settings settings;
3738 settings.fCaps = shaderCaps;
3739 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003740 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3741 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003742 &fStats, settings, &inputs);
3743 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003744
3745 str = fshaderTxt.c_str();
3746 length = SkToInt(fshaderTxt.size());
3747 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3748 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003749 &fStats, settings, &inputs);
3750 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003751
3752 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3753
3754 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3755 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3756 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3757 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3758
3759 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3760
3761 GL_CALL(DeleteShader(vshader));
3762 GL_CALL(DeleteShader(fshader));
3763
3764 return true;
3765}
3766
cdaltone2e71c22016-04-07 18:13:29 -07003767bool GrGLGpu::createWireRectProgram() {
3768 if (!fWireRectArrayBuffer) {
3769 static const GrGLfloat vdata[] = {
3770 0, 0,
3771 0, 1,
3772 1, 1,
3773 1, 0
3774 };
3775 fWireRectArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3776 kStatic_GrAccessPattern, vdata));
3777 if (!fWireRectArrayBuffer) {
3778 return false;
3779 }
3780 }
3781
bsalomon6dea83f2015-12-03 12:58:06 -08003782 SkASSERT(!fWireRectProgram.fProgram);
cdaltone2e71c22016-04-07 18:13:29 -07003783 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3784 if (!fWireRectProgram.fProgram) {
3785 return false;
3786 }
3787
Brian Salomon99938a82016-11-21 13:41:08 -05003788 GrShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3789 GrShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3790 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003791 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003792
3793 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3794 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3795 // uniform using the 4 vbo vertices.
3796 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003797 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003798 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003799 uRect.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003800 vshaderTxt.append(";");
3801 vshaderTxt.append(
3802 "// Wire Rect Program VS\n"
3803 "void main() {"
3804 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3805 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3806 " gl_Position.zw = vec2(0, 1);"
3807 "}"
3808 );
3809
Brian Salomon99938a82016-11-21 13:41:08 -05003810 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
bsalomon6dea83f2015-12-03 12:58:06 -08003811
3812 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -04003813 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision,
Brian Salomon1edc5b92016-11-29 13:43:46 -05003814 *this->caps()->shaderCaps(),
bsalomon6dea83f2015-12-03 12:58:06 -08003815 &fshaderTxt);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003816 uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003817 fshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003818 fshaderTxt.appendf(
3819 "// Write Rect Program FS\n"
3820 "void main() {"
ethannicholas5961bc92016-10-12 06:39:56 -07003821 " sk_FragColor = %s;"
bsalomon6dea83f2015-12-03 12:58:06 -08003822 "}",
bsalomon6dea83f2015-12-03 12:58:06 -08003823 uColor.c_str()
3824 );
3825
bsalomon6dea83f2015-12-03 12:58:06 -08003826 const char* str;
3827 GrGLint length;
3828
3829 str = vshaderTxt.c_str();
3830 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003831 SkSL::Program::Settings settings;
3832 settings.fCaps = this->caps()->shaderCaps();
3833 SkSL::Program::Inputs inputs;
bsalomon6dea83f2015-12-03 12:58:06 -08003834 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3835 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003836 &fStats, settings, &inputs);
3837 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003838
3839 str = fshaderTxt.c_str();
3840 length = SkToInt(fshaderTxt.size());
3841 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3842 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003843 &fStats, settings, &inputs);
3844 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003845
3846 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3847
3848 GL_CALL_RET(fWireRectProgram.fColorUniform,
3849 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3850 GL_CALL_RET(fWireRectProgram.fRectUniform,
3851 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3852 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3853
3854 GL_CALL(DeleteShader(vshader));
3855 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003856
3857 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003858}
3859
3860void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003861 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3862 // visualization.
3863
bsalomon6dea83f2015-12-03 12:58:06 -08003864 this->handleDirtyContext();
3865 if (!fWireRectProgram.fProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003866 if (!this->createWireRectProgram()) {
3867 SkDebugf("Failed to create wire rect program.\n");
3868 return;
3869 }
bsalomon6dea83f2015-12-03 12:58:06 -08003870 }
3871
3872 int w = rt->width();
3873 int h = rt->height();
3874
3875 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3876 // whether the render target is flipped or not.
3877 GrGLfloat edges[4];
3878 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3879 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3880 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3881 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3882 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3883 } else {
3884 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3885 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3886 }
3887 edges[0] = 2 * edges[0] / w - 1.0f;
3888 edges[1] = 2 * edges[1] / h - 1.0f;
3889 edges[2] = 2 * edges[2] / w - 1.0f;
3890 edges[3] = 2 * edges[3] / h - 1.0f;
3891
3892 GrGLfloat channels[4];
3893 static const GrGLfloat scale255 = 1.f / 255.f;
3894 channels[0] = GrColorUnpackR(color) * scale255;
3895 channels[1] = GrColorUnpackG(color) * scale255;
3896 channels[2] = GrColorUnpackB(color) * scale255;
3897 channels[3] = GrColorUnpackA(color) * scale255;
3898
3899 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3900 this->flushRenderTarget(glRT, &rect);
3901
3902 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3903 fHWProgramID = fWireRectProgram.fProgram;
3904
cdaltone2e71c22016-04-07 18:13:29 -07003905 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003906
cdaltone2e71c22016-04-07 18:13:29 -07003907 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003908 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05003909 attribs->set(this, 0, fWireRectArrayBuffer.get(), kVec2f_GrVertexAttribType,
3910 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003911
3912 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3913 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3914
3915 GrXferProcessor::BlendInfo blendInfo;
3916 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003917 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003918 this->flushColorWrite(true);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003919 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08003920 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003921 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003922 this->disableStencil();
bsalomon6dea83f2015-12-03 12:58:06 -08003923
3924 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3925}
3926
3927
cdaltone2e71c22016-04-07 18:13:29 -07003928bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07003929 GrSurface* src,
3930 const SkIRect& srcRect,
3931 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003932 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003933 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003934
3935 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003936 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003937 SkDebugf("Failed to create copy program.\n");
3938 return false;
3939 }
3940 }
3941
bsalomon6df86402015-06-01 10:41:49 -07003942 int w = srcRect.width();
3943 int h = srcRect.height();
3944
Brian Salomon514baff2016-11-17 15:17:07 -05003945 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07003946 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003947
bsalomon083617b2016-02-12 12:10:14 -08003948 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003949 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003950 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003951 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003952
bsalomon6df86402015-06-01 10:41:49 -07003953 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003954
bsalomon7ea33f52015-11-22 14:51:00 -08003955 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3956 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003957
cdaltone2e71c22016-04-07 18:13:29 -07003958 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003959
cdaltone2e71c22016-04-07 18:13:29 -07003960 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003961 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05003962 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
3963 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003964
3965 // dst rect edges in NDC (-1 to 1)
3966 int dw = dst->width();
3967 int dh = dst->height();
3968 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3969 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3970 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3971 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3972 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3973 dy0 = -dy0;
3974 dy1 = -dy1;
3975 }
3976
bsalomone5286e02016-01-14 09:24:09 -08003977 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3978 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3979 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3980 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003981 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003982 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003983 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003984 sy0 = sh - sy0;
3985 sy1 = sh - sy1;
3986 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04003987 // src rect edges in normalized texture space (0 to 1)
3988 sx0 /= sw;
3989 sx1 /= sw;
3990 sy0 /= sh;
3991 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003992
bsalomon7ea33f52015-11-22 14:51:00 -08003993 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3994 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3995 sx1 - sx0, sy1 - sy0, sx0, sy0));
3996 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003997
3998 GrXferProcessor::BlendInfo blendInfo;
3999 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004000 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004001 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08004002 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004003 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004004 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004005 this->disableStencil();
bsalomon6df86402015-06-01 10:41:49 -07004006
4007 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004008 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08004009 this->didWriteToSurface(dst, &dstRect);
4010
cdaltone2e71c22016-04-07 18:13:29 -07004011 return true;
bsalomon6df86402015-06-01 10:41:49 -07004012}
4013
4014void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
4015 GrSurface* src,
4016 const SkIRect& srcRect,
4017 const SkIPoint& dstPoint) {
4018 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07004019 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004020 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004021 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004022 SkASSERT(dstTex);
4023 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004024 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004025 GrGLIRect srcGLRect;
4026 srcGLRect.setRelativeTo(srcVP,
4027 srcRect.fLeft,
4028 srcRect.fTop,
4029 srcRect.width(),
4030 srcRect.height(),
4031 src->origin());
4032
4033 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004034 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004035 GrGLint dstY;
4036 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4037 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4038 } else {
4039 dstY = dstPoint.fY;
4040 }
bsalomon10528f12015-10-14 12:54:52 -07004041 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004042 dstPoint.fX, dstY,
4043 srcGLRect.fLeft, srcGLRect.fBottom,
4044 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004045 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004046 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4047 srcRect.width(), srcRect.height());
4048 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004049}
4050
4051bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
4052 GrSurface* src,
4053 const SkIRect& srcRect,
4054 const SkIPoint& dstPoint) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04004055 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07004056 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4057 srcRect.width(), srcRect.height());
4058 if (dst == src) {
4059 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4060 return false;
mtklein404b3b22015-05-18 09:29:10 -07004061 }
bsalomon5df6fee2015-05-18 06:26:15 -07004062 }
bsalomon6df86402015-06-01 10:41:49 -07004063
bsalomon6df86402015-06-01 10:41:49 -07004064 GrGLIRect dstVP;
4065 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004066 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4067 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004068 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004069 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004070 GrGLIRect srcGLRect;
4071 GrGLIRect dstGLRect;
4072 srcGLRect.setRelativeTo(srcVP,
4073 srcRect.fLeft,
4074 srcRect.fTop,
4075 srcRect.width(),
4076 srcRect.height(),
4077 src->origin());
4078 dstGLRect.setRelativeTo(dstVP,
4079 dstRect.fLeft,
4080 dstRect.fTop,
4081 dstRect.width(),
4082 dstRect.height(),
4083 dst->origin());
4084
4085 // BlitFrameBuffer respects the scissor, so disable it.
4086 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004087 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004088
4089 GrGLint srcY0;
4090 GrGLint srcY1;
4091 // Does the blit need to y-mirror or not?
4092 if (src->origin() == dst->origin()) {
4093 srcY0 = srcGLRect.fBottom;
4094 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4095 } else {
4096 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4097 srcY1 = srcGLRect.fBottom;
4098 }
4099 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4100 srcY0,
4101 srcGLRect.fLeft + srcGLRect.fWidth,
4102 srcY1,
4103 dstGLRect.fLeft,
4104 dstGLRect.fBottom,
4105 dstGLRect.fLeft + dstGLRect.fWidth,
4106 dstGLRect.fBottom + dstGLRect.fHeight,
4107 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004108 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4109 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004110 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004111 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004112}
4113
brianosman33f6b3f2016-06-02 05:49:21 -07004114// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4115// Uses draw calls to do a series of downsample operations to successive mips.
4116// If this returns false, then the calling code falls back to using glGenerateMipmap.
4117bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004118 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004119 // Our iterative downsample requires the ability to limit which level we're sampling:
4120 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004121 return false;
4122 }
4123
4124 // Mipmaps are only supported on 2D textures:
4125 if (GR_GL_TEXTURE_2D != texture->target()) {
4126 return false;
4127 }
4128
4129 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004130 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004131 return false;
4132 }
4133
brianosman33f6b3f2016-06-02 05:49:21 -07004134 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4135 if (GrPixelConfigIsSRGB(texture->config())) {
4136 // If we have write-control, just set the state that we want:
4137 if (this->glCaps().srgbWriteControl()) {
4138 this->flushFramebufferSRGB(gammaCorrect);
4139 } else if (!gammaCorrect) {
4140 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4141 return false;
4142 }
4143 }
4144
4145 int width = texture->width();
4146 int height = texture->height();
4147 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4148
4149 // Define all mips, if we haven't previously done so:
4150 if (0 == texture->texturePriv().maxMipMapLevel()) {
4151 GrGLenum internalFormat;
4152 GrGLenum externalFormat;
4153 GrGLenum externalType;
4154 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4155 &internalFormat, &externalFormat, &externalType)) {
4156 return false;
4157 }
4158
4159 for (GrGLint level = 1; level < levelCount; ++level) {
4160 // Define the next mip:
4161 width = SkTMax(1, width / 2);
4162 height = SkTMax(1, height / 2);
4163 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4164 width, height, 0,
4165 externalFormat, externalType, nullptr));
4166 }
4167 }
4168
4169 // Create (if necessary), then bind temporary FBO:
4170 if (0 == fTempDstFBOID) {
4171 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4172 }
4173 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004174 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004175
4176 // Bind the texture, to get things configured for filtering.
4177 // We'll be changing our base level further below:
4178 this->setTextureUnit(0);
Brian Salomon514baff2016-11-17 15:17:07 -05004179 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
brianosman33f6b3f2016-06-02 05:49:21 -07004180 this->bindTexture(0, params, gammaCorrect, texture);
4181
4182 // Vertex data:
4183 if (!fMipmapProgramArrayBuffer) {
4184 static const GrGLfloat vdata[] = {
4185 0, 0,
4186 0, 1,
4187 1, 0,
4188 1, 1
4189 };
4190 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4191 kVertex_GrBufferType,
4192 kStatic_GrAccessPattern, vdata));
4193 }
4194 if (!fMipmapProgramArrayBuffer) {
4195 return false;
4196 }
4197
4198 fHWVertexArrayState.setVertexArrayID(this, 0);
4199
4200 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004201 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05004202 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004203 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004204
4205 // Set "simple" state once:
4206 GrXferProcessor::BlendInfo blendInfo;
4207 blendInfo.reset();
4208 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4209 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004210 this->flushHWAAState(nullptr, false, false);
4211 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004212 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004213 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004214
4215 // Do all the blits:
4216 width = texture->width();
4217 height = texture->height();
4218 GrGLIRect viewport;
4219 viewport.fLeft = 0;
4220 viewport.fBottom = 0;
4221 for (GrGLint level = 1; level < levelCount; ++level) {
4222 // Get and bind the program for this particular downsample (filter shape can vary):
4223 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4224 if (!fMipmapPrograms[progIdx].fProgram) {
4225 if (!this->createMipmapProgram(progIdx)) {
4226 SkDebugf("Failed to create mipmap program.\n");
4227 return false;
4228 }
4229 }
4230 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4231 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4232
4233 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4234 const float invWidth = 1.0f / width;
4235 const float invHeight = 1.0f / height;
4236 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4237 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4238 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4239
4240 // Only sample from previous mip
4241 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4242
4243 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4244 GR_GL_TEXTURE_2D, texture->textureID(), level));
4245
4246 width = SkTMax(1, width / 2);
4247 height = SkTMax(1, height / 2);
4248 viewport.fWidth = width;
4249 viewport.fHeight = height;
4250 this->flushViewport(viewport);
4251
4252 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4253 }
4254
4255 // Unbind:
4256 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4257 GR_GL_TEXTURE_2D, 0, 0));
4258
4259 return true;
4260}
4261
csmartdaltonc25c5d72016-11-01 07:03:59 -07004262void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
4263 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004264 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4265 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004266
4267 this->flushStencil(stencil);
4268 this->flushHWAAState(rt, true, !stencil.isDisabled());
4269 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4270
4271 if (0 != this->caps()->maxRasterSamples()) {
4272 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4273 } else {
4274 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4275 }
4276
Brian Salomon154ce912017-05-17 10:40:02 -04004277 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004278
4279 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004280 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004281 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4282 GrGLfloat pos[2];
4283 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4284 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004285 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004286 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004287 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004288 }
4289 }
4290 }
4291}
4292
cdalton231c5fd2015-05-13 12:35:36 -07004293void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004294 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004295 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004296 case kTexture_GrXferBarrierType: {
4297 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4298 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4299 // The render target uses separate storage so no need for glTextureBarrier.
4300 // FIXME: The render target will resolve automatically when its texture is bound,
4301 // but we could resolve only the bounds that will be read if we do it here instead.
4302 return;
4303 }
cdalton9954bc32015-04-29 14:17:00 -07004304 SkASSERT(this->caps()->textureBarrierSupport());
4305 GL_CALL(TextureBarrier());
4306 return;
cdalton231c5fd2015-05-13 12:35:36 -07004307 }
cdalton8917d622015-05-06 13:40:21 -07004308 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004309 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004310 this->caps()->blendEquationSupport());
4311 GL_CALL(BlendBarrier());
4312 return;
bsalomoncb02b382015-08-12 11:14:50 -07004313 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004314 }
4315}
4316
jvanverth88957922015-07-14 11:02:52 -07004317GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004318 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004319 if (!this->caps()->isConfigTexturable(config)) {
4320 return false;
4321 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004322 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004323 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004324 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004325 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004326 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4327 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004328 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004329 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004330 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4331 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4332 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4333 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004334
bsalomon76148af2016-01-12 11:13:47 -08004335 GrGLenum internalFormat;
4336 GrGLenum externalFormat;
4337 GrGLenum externalType;
4338
4339 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4340 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004341 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004342 }
jvanverth672bb7f2015-07-13 07:19:57 -07004343
bsalomon091f60c2015-11-10 11:54:56 -08004344 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004345 externalType, pixels));
4346
Ben Wagner18b61f92016-10-25 10:44:02 -04004347 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004348}
4349
jvanverth88957922015-07-14 11:02:52 -07004350bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004351 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004352
4353 GrGLboolean result;
4354 GL_CALL_RET(result, IsTexture(texID));
4355
4356 return (GR_GL_TRUE == result);
4357}
4358
bsalomone63ffef2016-02-05 07:17:34 -08004359void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004360 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004361 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004362
bsalomon67d76202015-11-11 12:40:42 -08004363 if (!abandonTexture) {
4364 GL_CALL(DeleteTextures(1, &texID));
4365 }
jvanverth672bb7f2015-07-13 07:19:57 -07004366}
4367
joshualitt8fd844f2015-12-02 13:36:47 -08004368void GrGLGpu::resetShaderCacheForTesting() const {
4369 fProgramCache->abandon();
4370}
4371
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004372///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004373
cdaltone2e71c22016-04-07 18:13:29 -07004374GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004375 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004376 GrGLAttribArrayState* attribState;
4377
cdaltone2e71c22016-04-07 18:13:29 -07004378 if (gpu->glCaps().isCoreProfile()) {
4379 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004380 GrGLuint arrayID;
4381 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4382 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004383 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004384 }
cdaltone2e71c22016-04-07 18:13:29 -07004385 if (ibuf) {
4386 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004387 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004388 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004389 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004390 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004391 if (ibuf) {
4392 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4393 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004394 } else {
4395 this->setVertexArrayID(gpu, 0);
4396 }
4397 int attrCount = gpu->glCaps().maxVertexAttributes();
4398 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4399 fDefaultVertexArrayAttribState.resize(attrCount);
4400 }
4401 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004402 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004403 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004404}
bsalomone179a912016-01-20 06:18:10 -08004405
Robert Phillips3798c862017-03-27 11:08:16 -04004406bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Robert Phillips81444fb2017-03-21 09:14:35 -04004407 const GrSamplerParams& textureParams,
4408 GrTextureProducer::CopyParams* copyParams,
4409 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004410 const GrTexture* texture = proxy->priv().peekTexture();
4411 if (!texture) {
4412 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4413 // In that case the proxy should already be instantiated.
4414 return false;
4415 }
4416
bsalomone179a912016-01-20 06:18:10 -08004417 if (textureParams.isTiled() ||
Brian Salomon514baff2016-11-17 15:17:07 -05004418 GrSamplerParams::kMipMap_FilterMode == textureParams.filterMode()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004419 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004420 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4421 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon514baff2016-11-17 15:17:07 -05004422 copyParams->fFilter = GrSamplerParams::kNone_FilterMode;
bsalomone179a912016-01-20 06:18:10 -08004423 copyParams->fWidth = texture->width();
4424 copyParams->fHeight = texture->height();
4425 return true;
4426 }
4427 }
4428 return false;
4429}
jvanverth84741b32016-09-30 08:39:02 -07004430
Greg Daniel6be35232017-03-01 17:01:09 -05004431GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
4432 GrGLsync sync;
4433 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4434 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4435 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004436}
4437
Greg Daniel6be35232017-03-01 17:01:09 -05004438bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004439 GrGLenum result;
4440 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4441 return (GR_GL_CONDITION_SATISFIED == result);
4442}
4443
4444void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004445 this->deleteSync((GrGLsync)fence);
4446}
4447
4448sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore() {
4449 return GrGLSemaphore::Make(this);
4450}
4451
Brian Osmandc87c952017-04-28 13:57:38 -04004452void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004453 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4454
4455 GrGLsync sync;
4456 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4457 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004458
4459 if (flush) {
4460 GL_CALL(Flush());
4461 }
Greg Daniel6be35232017-03-01 17:01:09 -05004462}
4463
4464void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4465 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4466
4467 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4468}
4469
4470void GrGLGpu::deleteSync(GrGLsync sync) const {
4471 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004472}
Brian Osman13dddce2017-05-09 13:19:50 -04004473
4474sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4475 // Set up a semaphore to be signaled once the data is ready, and flush GL
4476 sk_sp<GrSemaphore> semaphore = this->makeSemaphore();
4477 this->insertSemaphore(semaphore, true);
4478
4479 return semaphore;
4480}