blob: 9ccd073fe7ab29818e0d8f7318cce7cc92c7c1ce [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"
cdalton397536c2016-03-25 12:15:03 -07009#include "GrGLBuffer.h"
jvanverthcba99b82015-06-24 06:59:57 -070010#include "GrGLGLSL.h"
egdaniel066df7c2016-06-08 14:02:27 -070011#include "GrGLGpuCommandBuffer.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070012#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080013#include "GrGLTextureRenderTarget.h"
csmartdalton29df7602016-08-31 11:55:52 -070014#include "GrFixedClip.h"
bsalomon3582d3e2015-02-13 14:20:05 -080015#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070016#include "GrMesh.h"
egdaniel8dd688b2015-01-22 10:16:09 -080017#include "GrPipeline.h"
ethannicholas22793252016-01-30 09:59:10 -080018#include "GrPLSGeometryProcessor.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070021#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000022#include "GrTypes.h"
bsalomon6df86402015-06-01 10:41:49 -070023#include "builders/GrGLShaderStringBuilder.h"
egdanielcb7ba1e2015-10-26 08:38:25 -070024#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070025#include "glsl/GrGLSLCaps.h"
ethannicholas22793252016-01-30 09:59:10 -080026#include "glsl/GrGLSLPLSPathRendering.h"
csmartdaltona7f29642016-07-07 08:49:11 -070027#include "instanced/GLInstancedRendering.h"
Ben Wagner18b61f92016-10-25 10:44:02 -040028#include "SkMakeUnique.h"
cblume55f2d2d2016-02-26 13:20:48 -080029#include "SkMipMap.h"
30#include "SkPixmap.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000031#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000032#include "SkTemplates.h"
cblume55f2d2d2016-02-26 13:20:48 -080033#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000034
bsalomon@google.com0b77d682011-08-19 13:28:54 +000035#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000036#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000037
reed@google.comac10a2d2010-12-22 21:39:39 +000038#define SKIP_CACHE_CHECK true
39
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000040#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
41 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
42 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
43 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000045 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
46 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
47 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
48#endif
49
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000050///////////////////////////////////////////////////////////////////////////////
51
csmartdaltona7f29642016-07-07 08:49:11 -070052using gr_instanced::InstancedRendering;
53using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070054
cdalton8917d622015-05-06 13:40:21 -070055static const GrGLenum gXfermodeEquation2Blend[] = {
56 // Basic OpenGL blend equations.
57 GR_GL_FUNC_ADD,
58 GR_GL_FUNC_SUBTRACT,
59 GR_GL_FUNC_REVERSE_SUBTRACT,
60
61 // GL_KHR_blend_equation_advanced.
62 GR_GL_SCREEN,
63 GR_GL_OVERLAY,
64 GR_GL_DARKEN,
65 GR_GL_LIGHTEN,
66 GR_GL_COLORDODGE,
67 GR_GL_COLORBURN,
68 GR_GL_HARDLIGHT,
69 GR_GL_SOFTLIGHT,
70 GR_GL_DIFFERENCE,
71 GR_GL_EXCLUSION,
72 GR_GL_MULTIPLY,
73 GR_GL_HSL_HUE,
74 GR_GL_HSL_SATURATION,
75 GR_GL_HSL_COLOR,
76 GR_GL_HSL_LUMINOSITY
77};
78GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
79GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
80GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
81GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
82GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
83GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
84GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
85GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
86GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
87GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
88GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
89GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
90GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
91GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
92GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
93GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
94GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
95GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070096GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070097
twiz@google.com0f31ca72011-03-18 17:38:11 +000098static const GrGLenum gXfermodeCoeff2Blend[] = {
99 GR_GL_ZERO,
100 GR_GL_ONE,
101 GR_GL_SRC_COLOR,
102 GR_GL_ONE_MINUS_SRC_COLOR,
103 GR_GL_DST_COLOR,
104 GR_GL_ONE_MINUS_DST_COLOR,
105 GR_GL_SRC_ALPHA,
106 GR_GL_ONE_MINUS_SRC_ALPHA,
107 GR_GL_DST_ALPHA,
108 GR_GL_ONE_MINUS_DST_ALPHA,
109 GR_GL_CONSTANT_COLOR,
110 GR_GL_ONE_MINUS_CONSTANT_COLOR,
111 GR_GL_CONSTANT_ALPHA,
112 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000113
114 // extended blend coeffs
115 GR_GL_SRC1_COLOR,
116 GR_GL_ONE_MINUS_SRC1_COLOR,
117 GR_GL_SRC1_ALPHA,
118 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000119};
120
bsalomon861e1032014-12-16 07:33:49 -0800121bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000122 static const bool gCoeffReferencesBlendConst[] = {
123 false,
124 false,
125 false,
126 false,
127 false,
128 false,
129 false,
130 false,
131 false,
132 false,
133 true,
134 true,
135 true,
136 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000137
138 // extended blend coeffs
139 false,
140 false,
141 false,
142 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000143 };
144 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700145 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000146
bsalomon@google.com47059542012-06-06 20:51:20 +0000147 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
148 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
149 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
150 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
151 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
152 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
153 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
154 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
155 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
156 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
157 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
158 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
159 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
160 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161
bsalomon@google.com47059542012-06-06 20:51:20 +0000162 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
163 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
164 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
165 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000166
167 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700168 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000169}
170
reed@google.comac10a2d2010-12-22 21:39:39 +0000171///////////////////////////////////////////////////////////////////////////////
172
egdanielff1d5472015-09-10 08:37:20 -0700173
bsalomon682c2692015-05-22 14:01:46 -0700174GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
175 GrContext* context) {
Hal Canary144caf52016-11-07 17:57:18 -0500176 sk_sp<const GrGLInterface> glInterface(
bsalomon424cc262015-05-22 10:37:30 -0700177 reinterpret_cast<const GrGLInterface*>(backendContext));
178 if (!glInterface) {
179 glInterface.reset(GrGLDefaultInterface());
180 } else {
181 glInterface->ref();
182 }
183 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700184 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700185 }
Hal Canary144caf52016-11-07 17:57:18 -0500186 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700187 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700188 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700189 }
halcanary96fcdcc2015-08-27 07:41:13 -0700190 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700191}
192
rileya@google.come38160c2012-07-03 18:03:04 +0000193static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000194
bsalomon424cc262015-05-22 10:37:30 -0700195GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000196 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700197 , fGLContext(ctx)
198 , fProgramCache(new ProgramCache(this))
199 , fHWProgramID(0)
200 , fTempSrcFBOID(0)
201 , fTempDstFBOID(0)
202 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700203 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700204 , fHWPLSEnabled(false)
205 , fPLSHasBeenUsed(false)
206 , fHWMinSampleShading(0.0) {
207 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
208 fCopyPrograms[i].fProgram = 0;
209 }
brianosman33f6b3f2016-06-02 05:49:21 -0700210 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
211 fMipmapPrograms[i].fProgram = 0;
212 }
cdaltone2e71c22016-04-07 18:13:29 -0700213 fWireRectProgram.fProgram = 0;
214 fPLSSetupProgram.fProgram = 0;
215
bsalomon424cc262015-05-22 10:37:30 -0700216 SkASSERT(ctx);
217 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000218
cdalton9c3f1432016-03-11 10:07:37 -0800219 fHWBoundTextureUniqueIDs.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000220
cdaltone2e71c22016-04-07 18:13:29 -0700221 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
222 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
223 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
224 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
225 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
226 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
227 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
228 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
229 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
230 } else {
231 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
232 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
233 }
234 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
235
cdalton74b8d322016-04-11 14:47:28 -0700236 if (this->caps()->shaderCaps()->texelBufferSupport()) {
237 fHWBufferTextures.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
238 }
239
cdaltone2e71c22016-04-07 18:13:29 -0700240 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
241 fPathRendering.reset(new GrGLPathRendering(this));
242 }
243
bsalomon424cc262015-05-22 10:37:30 -0700244 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000245 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000246 const GrGLubyte* vendor;
247 const GrGLubyte* renderer;
248 const GrGLubyte* version;
249 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
250 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
251 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800252 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000253 this);
tfarina38406c82014-10-31 07:11:12 -0700254 SkDebugf("------ VENDOR %s\n", vendor);
255 SkDebugf("------ RENDERER %s\n", renderer);
256 SkDebugf("------ VERSION %s\n", version);
257 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700258 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700259 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800260 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000261 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000262}
263
bsalomon861e1032014-12-16 07:33:49 -0800264GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700265 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
266 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800267 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700268 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700269 fMipmapProgramArrayBuffer.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700270 fWireRectArrayBuffer.reset();
271 fPLSSetupProgram.fArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800272
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000273 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000274 // detach the current program so there is no confusion on OpenGL's part
275 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000276 GL_CALL(UseProgram(0));
277 }
278
egdanield803f272015-03-18 13:01:52 -0700279 if (0 != fTempSrcFBOID) {
280 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800281 }
egdanield803f272015-03-18 13:01:52 -0700282 if (0 != fTempDstFBOID) {
283 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800284 }
egdanield803f272015-03-18 13:01:52 -0700285 if (0 != fStencilClearFBOID) {
286 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800287 }
egdaniel0f5f9672015-02-03 11:10:51 -0800288
bsalomon7ea33f52015-11-22 14:51:00 -0800289 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
290 if (0 != fCopyPrograms[i].fProgram) {
291 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
292 }
bsalomon6df86402015-06-01 10:41:49 -0700293 }
bsalomon6dea83f2015-12-03 12:58:06 -0800294
brianosman33f6b3f2016-06-02 05:49:21 -0700295 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
296 if (0 != fMipmapPrograms[i].fProgram) {
297 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
298 }
299 }
300
bsalomon6dea83f2015-12-03 12:58:06 -0800301 if (0 != fWireRectProgram.fProgram) {
302 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
303 }
304
ethannicholas22793252016-01-30 09:59:10 -0800305 if (0 != fPLSSetupProgram.fProgram) {
306 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
307 }
308
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000309 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700310}
311
cdaltone2e71c22016-04-07 18:13:29 -0700312bool GrGLGpu::createPLSSetupProgram() {
313 if (!fPLSSetupProgram.fArrayBuffer) {
314 static const GrGLfloat vdata[] = {
315 0, 0,
316 0, 1,
317 1, 0,
318 1, 1
319 };
320 fPLSSetupProgram.fArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
321 kVertex_GrBufferType,
322 kStatic_GrAccessPattern, vdata));
323 if (!fPLSSetupProgram.fArrayBuffer) {
324 return false;
325 }
326 }
327
328 SkASSERT(!fPLSSetupProgram.fProgram);
329 GL_CALL_RET(fPLSSetupProgram.fProgram, CreateProgram());
330 if (!fPLSSetupProgram.fProgram) {
331 return false;
332 }
333
cdaltonc08f1962016-02-12 12:14:06 -0800334 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
335 const char* version = glslCaps->versionDeclString();
ethannicholas22793252016-01-30 09:59:10 -0800336
337 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
338 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
339 GrShaderVar::kUniform_TypeModifier);
340 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
egdaniel990dbc82016-07-13 14:09:30 -0700341 GrGLSLShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
342 GrShaderVar::kUniform_TypeModifier);
ethannicholas22793252016-01-30 09:59:10 -0800343 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
halcanary9d524f22016-03-29 09:03:52 -0700344
ethannicholas22793252016-01-30 09:59:10 -0800345 SkString vshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800346 if (glslCaps->noperspectiveInterpolationSupport()) {
347 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
348 vshaderTxt.appendf("#extension %s : require\n", extension);
349 }
350 vTexCoord.addModifier("noperspective");
351 }
352 aVertex.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800353 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800354 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800355 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800356 uPosXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800357 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800358 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800359 vshaderTxt.append(";");
halcanary9d524f22016-03-29 09:03:52 -0700360
ethannicholas22793252016-01-30 09:59:10 -0800361 vshaderTxt.append(
362 "// PLS Setup Program VS\n"
363 "void main() {"
364 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
365 " gl_Position.zw = vec2(0, 1);"
366 "}"
367 );
368
369 SkString fshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800370 if (glslCaps->noperspectiveInterpolationSupport()) {
371 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
372 fshaderTxt.appendf("#extension %s : require\n", extension);
373 }
374 }
ethannicholas22793252016-01-30 09:59:10 -0800375 fshaderTxt.append("#extension ");
cdaltonc08f1962016-02-12 12:14:06 -0800376 fshaderTxt.append(glslCaps->fbFetchExtensionString());
ethannicholas22793252016-01-30 09:59:10 -0800377 fshaderTxt.append(" : require\n");
378 fshaderTxt.append("#extension GL_EXT_shader_pixel_local_storage : require\n");
cdaltonc08f1962016-02-12 12:14:06 -0800379 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800380 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
cdaltonc08f1962016-02-12 12:14:06 -0800381 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800382 fshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800383 uTexture.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800384 fshaderTxt.append(";");
385
386 fshaderTxt.appendf(
387 "// PLS Setup Program FS\n"
388 GR_GL_PLS_PATH_DATA_DECL
389 "void main() {\n"
390 " " GR_GL_PLS_DSTCOLOR_NAME " = gl_LastFragColorARM;\n"
391 " pls.windings = ivec4(0, 0, 0, 0);\n"
392 "}"
393 );
cdaltone2e71c22016-04-07 18:13:29 -0700394
ethannicholas22793252016-01-30 09:59:10 -0800395 const char* str;
396 GrGLint length;
397
398 str = vshaderTxt.c_str();
399 length = SkToInt(vshaderTxt.size());
400 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
401 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
402
403 str = fshaderTxt.c_str();
404 length = SkToInt(fshaderTxt.size());
405 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
406 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
407
408 GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
409
410 GL_CALL_RET(fPLSSetupProgram.fPosXformUniform, GetUniformLocation(fPLSSetupProgram.fProgram,
411 "u_posXform"));
412
413 GL_CALL(BindAttribLocation(fPLSSetupProgram.fProgram, 0, "a_vertex"));
414
415 GL_CALL(DeleteShader(vshader));
416 GL_CALL(DeleteShader(fshader));
417
cdaltone2e71c22016-04-07 18:13:29 -0700418 return true;
ethannicholas22793252016-01-30 09:59:10 -0800419}
420
bsalomon6e2aad42016-04-01 11:54:31 -0700421void GrGLGpu::disconnect(DisconnectType type) {
422 INHERITED::disconnect(type);
423 if (DisconnectType::kCleanup == type) {
424 if (fHWProgramID) {
425 GL_CALL(UseProgram(0));
426 }
427 if (fTempSrcFBOID) {
428 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
429 }
430 if (fTempDstFBOID) {
431 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
432 }
433 if (fStencilClearFBOID) {
434 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
435 }
bsalomon6e2aad42016-04-01 11:54:31 -0700436 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
437 if (fCopyPrograms[i].fProgram) {
438 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
439 }
440 }
brianosman33f6b3f2016-06-02 05:49:21 -0700441 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
442 if (fMipmapPrograms[i].fProgram) {
443 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
444 }
445 }
bsalomon6e2aad42016-04-01 11:54:31 -0700446 if (fWireRectProgram.fProgram) {
447 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
448 }
bsalomon6e2aad42016-04-01 11:54:31 -0700449 if (fPLSSetupProgram.fProgram) {
450 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
451 }
bsalomon6e2aad42016-04-01 11:54:31 -0700452 } else {
453 if (fProgramCache) {
454 fProgramCache->abandon();
455 }
456 }
457
458 delete fProgramCache;
459 fProgramCache = nullptr;
460
bsalomonc8dc1f72014-08-21 13:02:13 -0700461 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700462 fTempSrcFBOID = 0;
463 fTempDstFBOID = 0;
464 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700465 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800466 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
467 fCopyPrograms[i].fProgram = 0;
468 }
brianosman33f6b3f2016-06-02 05:49:21 -0700469 fMipmapProgramArrayBuffer.reset();
470 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
471 fMipmapPrograms[i].fProgram = 0;
472 }
bsalomon6dea83f2015-12-03 12:58:06 -0800473 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700474 fWireRectArrayBuffer.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700475 fPLSSetupProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700476 fPLSSetupProgram.fArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700477 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700478 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700479 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000480}
481
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000482///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000483
bsalomon861e1032014-12-16 07:33:49 -0800484void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000485 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000486 if (resetBits & kMisc_GrGLBackendState) {
487 GL_CALL(Disable(GR_GL_DEPTH_TEST));
488 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000489
cdaltone2e71c22016-04-07 18:13:29 -0700490 fHWBufferState[kTexel_GrBufferType].invalidate();
491 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
492 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
493 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700494
robertphillips5fa7f302016-07-21 09:21:04 -0700495 fHWDrawFace = GrDrawFace::kInvalid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000497 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000498 // Desktop-only state that we never change
499 if (!this->glCaps().isCoreProfile()) {
500 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
501 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
502 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
503 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
504 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
505 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
506 }
507 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
508 // core profile. This seems like a bug since the core spec removes any mention of
509 // GL_ARB_imaging.
510 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
511 GL_CALL(Disable(GR_GL_COLOR_TABLE));
512 }
513 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
514 // Since ES doesn't support glPointSize at all we always use the VS to
515 // set the point size
516 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
517
518 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
519 // currently part of our gl interface. There are probably others as
520 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000521 }
joshualitt58162332014-08-01 06:44:53 -0700522
523 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700524 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700525 // The arm extension requires specifically enabling MSAA fetching per sample.
526 // On some devices this may have a perf hit. Also multiple render targets are disabled
527 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
528 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000529 fHWWriteToColor = kUnknown_TriState;
530 // we only ever use lines in hairline mode
531 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700532 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000533 }
edisonn@google.comba669992013-06-28 16:03:21 +0000534
egdanielb414f252014-07-29 13:15:47 -0700535 if (resetBits & kMSAAEnable_GrGLBackendState) {
536 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700537
egdanieleed519e2016-01-15 11:36:18 -0800538 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800539 if (0 != this->caps()->maxRasterSamples()) {
540 fHWRasterMultisampleEnabled = kUnknown_TriState;
541 fHWNumRasterSamples = 0;
542 }
543
544 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
545 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700546 GL_CALL(CoverageModulation(GR_GL_RGBA));
547 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000548 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000549
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000550 fHWActiveTextureUnitIdx = -1; // invalid
551
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000552 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700553 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
554 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000555 }
cdalton74b8d322016-04-11 14:47:28 -0700556 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
557 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
558 fHWBufferTextures[b].fKnownBound = false;
559 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000560 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000561
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000562 if (resetBits & kBlend_GrGLBackendState) {
563 fHWBlendState.invalidate();
564 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000565
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000566 if (resetBits & kView_GrGLBackendState) {
567 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700568 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 fHWViewport.invalidate();
570 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000572 if (resetBits & kStencil_GrGLBackendState) {
573 fHWStencilSettings.invalidate();
574 fHWStencilTestEnabled = kUnknown_TriState;
575 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000576
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000577 // Vertex
578 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700579 fHWVertexArrayState.invalidate();
580 fHWBufferState[kVertex_GrBufferType].invalidate();
581 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000582 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000584 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700585 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700586 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000587 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000588
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000589 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700590 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700591 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000593 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000594
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000595 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000596 if (resetBits & kPixelStore_GrGLBackendState) {
597 if (this->glCaps().unpackRowLengthSupport()) {
598 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
599 }
600 if (this->glCaps().packRowLengthSupport()) {
601 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
602 }
603 if (this->glCaps().unpackFlipYSupport()) {
604 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
605 }
606 if (this->glCaps().packFlipYSupport()) {
607 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
608 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000609 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000610
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000611 if (resetBits & kProgram_GrGLBackendState) {
612 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000613 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000614}
615
egdanielcf614fd2015-04-22 13:58:58 -0700616static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000617 // By default, GrRenderTargets are GL's normal orientation so that they
618 // can be drawn to by the outside world without the client having
619 // to render upside down.
620 if (kDefault_GrSurfaceOrigin == origin) {
621 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
622 } else {
623 return origin;
624 }
625}
626
bungeman6bd52842016-10-27 09:30:08 -0700627sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
628 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800629 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
630 if (!info || !info->fID) {
631 return nullptr;
632 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000633
bsalomon7ea33f52015-11-22 14:51:00 -0800634 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
635 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
636
bsalomonb15b4c12014-10-29 12:41:57 -0700637 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -0800638 idDesc.fInfo = *info;
bsalomone5286e02016-01-14 09:24:09 -0800639
bsalomon7ea33f52015-11-22 14:51:00 -0800640 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
641 if (renderTarget) {
642 // This combination is not supported.
643 return nullptr;
644 }
cdalton9c3f1432016-03-11 10:07:37 -0800645 if (!this->glCaps().glslCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800646 return nullptr;
647 }
bsalomone5286e02016-01-14 09:24:09 -0800648 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
649 if (!this->glCaps().rectangleTextureSupport()) {
650 return nullptr;
651 }
652 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
653 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800654 }
bsalomone5286e02016-01-14 09:24:09 -0800655
656 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800657 // for a render buffer that resolves to the texture. We don't support MSAA textures.
658 if (desc.fSampleCnt && !renderTarget) {
659 return nullptr;
660 }
bsalomon10528f12015-10-14 12:54:52 -0700661
kkinnunen2e6055b2016-04-22 01:48:29 -0700662 if (kAdopt_GrWrapOwnership == ownership) {
663 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
664 } else {
665 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
bsalomone5286e02016-01-14 09:24:09 -0800666 }
bsalomonb15b4c12014-10-29 12:41:57 -0700667
Ben Wagner18b61f92016-10-25 10:44:02 -0400668 GrSurfaceDesc surfDesc;
bsalomonb15b4c12014-10-29 12:41:57 -0700669 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
670 surfDesc.fWidth = desc.fWidth;
671 surfDesc.fHeight = desc.fHeight;
672 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700673 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000674 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
675 // assuming the old behaviour, which is that backend textures are always
676 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
677 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
678 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700679 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000680 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700681 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000682 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000683
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000684 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700685 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700686 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700687 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000688 }
bungeman6bd52842016-10-27 09:30:08 -0700689 return GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000690 }
bungeman6bd52842016-10-27 09:30:08 -0700691 return GrGLTexture::MakeWrapped(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000692}
693
bungeman6bd52842016-10-27 09:30:08 -0700694sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
695 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700696 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700697 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700698 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700699 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
kkinnunen2e6055b2016-04-22 01:48:29 -0700700 if (kAdopt_GrWrapOwnership == ownership) {
701 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
702 } else {
703 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
cblume61214052016-01-26 09:10:48 -0800704 }
csmartdaltonf9635992016-08-10 11:09:07 -0700705 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000706
bsalomonb15b4c12014-10-29 12:41:57 -0700707 GrSurfaceDesc desc;
708 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700709 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700710 desc.fWidth = wrapDesc.fWidth;
711 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700712 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700713 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
714
bungeman6bd52842016-10-27 09:30:08 -0700715 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000716}
717
bungeman6bd52842016-10-27 09:30:08 -0700718sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc){
ericrkf7b8b8a2016-02-24 14:49:51 -0800719 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
720 if (!info || !info->fID) {
721 return nullptr;
722 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800723
jvanverthdb1adf52016-05-03 05:37:45 -0700724 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700725 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800726
jvanverthdb1adf52016-05-03 05:37:45 -0700727 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
728 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800729 // Only texture rectangle and texture 2d are supported. We do not check whether texture
730 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
731 // we assume the necessary support exists.
732 return nullptr;
733 }
734
Ben Wagner18b61f92016-10-25 10:44:02 -0400735 GrSurfaceDesc surfDesc;
ericrkf7b8b8a2016-02-24 14:49:51 -0800736 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
737 surfDesc.fWidth = desc.fWidth;
738 surfDesc.fHeight = desc.fHeight;
739 surfDesc.fConfig = desc.fConfig;
740 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
741 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
742 // assuming the old behaviour, which is that backend textures are always
743 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
744 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
745 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
746 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
747 } else {
748 surfDesc.fOrigin = desc.fOrigin;
749 }
750
751 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700752 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800753 return nullptr;
754 }
bungeman6bd52842016-10-27 09:30:08 -0700755 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800756}
757
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000758////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800759
bsalomonf0674512015-07-28 13:26:15 -0700760bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800761 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700762 DrawPreference* drawPreference,
763 WritePixelTempDrawInfo* tempDrawInfo) {
764 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
765 return false;
766 }
767
bsalomon6cb3cbe2015-07-30 07:34:27 -0700768 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
769 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
770 if (!dstSurface->asTexture()) {
771 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800772 } else {
773 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800774 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
775 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800776 return false;
777 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700778 }
779
bsalomon16921ec2015-07-30 15:34:56 -0700780 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
781 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
782 }
783
bsalomon6c9cd552016-01-22 07:17:34 -0800784 // Start off assuming no swizzling
785 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
786 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700787
788 // These settings we will always want if a temp draw is performed. Initially set the config
789 // to srcConfig, though that may be modified if we decide to do a R/G swap.
790 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
791 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
792 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
793 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
794 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
795 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
796
797 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
798
799 if (configsAreRBSwaps) {
800 if (!this->caps()->isConfigTexturable(srcConfig)) {
801 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
802 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800803 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
804 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700805 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
806 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700807 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
808 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800809 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
810 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700811 } else if (kGLES_GrGLStandard == this->glStandard() &&
812 this->glCaps().bgraIsInternalFormat()) {
813 // The internal format and external formats must match texture uploads so we can't
814 // swizzle while uploading when BGRA is a distinct internal format.
815 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
816 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800817 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
818 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700819 }
820 }
821
822 if (!this->glCaps().unpackFlipYSupport() &&
823 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
824 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
825 }
826
827 return true;
828}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000829
jvanverth17aa0472016-01-05 10:41:27 -0800830static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
831 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700832 if (!glTex) {
833 return false;
834 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000835
bsalomon16921ec2015-07-30 15:34:56 -0700836 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
837 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
838 return false;
839 }
840
bsalomone5286e02016-01-14 09:24:09 -0800841 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
842 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800843 return false;
844 }
845
jvanverth17aa0472016-01-05 10:41:27 -0800846 return true;
847}
848
849bool GrGLGpu::onWritePixels(GrSurface* surface,
850 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800851 GrPixelConfig config,
852 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800853 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
854
855 if (!check_write_and_transfer_input(glTex, surface, config)) {
856 return false;
857 }
858
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000859 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700860 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000861
krajcevski145d48c2014-06-11 16:07:50 -0700862 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700863 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800864 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700865 SkASSERT(config == glTex->desc().fConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800866 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800867 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700868 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800869 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800870 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700871 }
872
cblume55f2d2d2016-02-26 13:20:48 -0800873 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000874}
875
jvanverthc3d706f2016-04-20 10:33:27 -0700876bool GrGLGpu::onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800877 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700878 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800879 size_t offset, size_t rowBytes) {
jvanverthc3d706f2016-04-20 10:33:27 -0700880 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800881
jvanverthc3d706f2016-04-20 10:33:27 -0700882 if (!check_write_and_transfer_input(glTex, surface, config)) {
jvanverth17aa0472016-01-05 10:41:27 -0800883 return false;
884 }
885
886 // For the moment, can't transfer compressed data
887 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
888 return false;
889 }
890
891 this->setScratchTextureUnit();
892 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
893
cdalton397536c2016-03-25 12:15:03 -0700894 SkASSERT(!transferBuffer->isMapped());
895 SkASSERT(!transferBuffer->isCPUBacked());
896 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700897 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800898
899 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800900 GrMipLevel mipLevel;
jvanverthc3d706f2016-04-20 10:33:27 -0700901 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800902 mipLevel.fRowBytes = rowBytes;
903 SkSTArray<1, GrMipLevel> texels;
904 texels.push_back(mipLevel);
jcgregorioe7d7f902016-02-04 19:51:25 -0800905 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800906 left, top, width, height, config, texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700907 return success;
jvanverth17aa0472016-01-05 10:41:27 -0800908}
909
bsalomonf46a1242015-12-15 12:37:38 -0800910// For GL_[UN]PACK_ALIGNMENT.
911static inline GrGLint config_alignment(GrPixelConfig config) {
912 SkASSERT(!GrPixelConfigIsCompressed(config));
913 switch (config) {
914 case kAlpha_8_GrPixelConfig:
915 return 1;
916 case kRGB_565_GrPixelConfig:
917 case kRGBA_4444_GrPixelConfig:
918 case kAlpha_half_GrPixelConfig:
919 case kRGBA_half_GrPixelConfig:
920 return 2;
921 case kRGBA_8888_GrPixelConfig:
922 case kBGRA_8888_GrPixelConfig:
923 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700924 case kSBGRA_8888_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800925 case kRGBA_float_GrPixelConfig:
926 return 4;
927 default:
928 return 0;
929 }
930}
931
bsalomonb15b4c12014-10-29 12:41:57 -0700932static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
933 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700934 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000935 return GR_GL_GET_ERROR(interface);
936 } else {
937 return CHECK_ALLOC_ERROR(interface);
938 }
939}
940
cblume55f2d2d2016-02-26 13:20:48 -0800941/**
942 * Creates storage space for the texture and fills it with texels.
943 *
944 * @param desc The surface descriptor for the texture being created.
945 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800946 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700947 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
948 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800949 * @param externalFormat The data format used for the external storage of the texture.
950 * @param externalType The type of the data used for the external storage of the texture.
951 * @param texels The texel data of the texture being created.
952 * @param baseWidth The width of the texture's base mipmap level
953 * @param baseHeight The height of the texture's base mipmap level
954 * @param succeeded Set to true if allocating and populating the texture completed
955 * without error.
956 */
bsalomone699d0c2016-03-09 06:25:15 -0800957static bool allocate_and_populate_uncompressed_texture(const GrSurfaceDesc& desc,
cblume55f2d2d2016-02-26 13:20:48 -0800958 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800959 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800960 GrGLenum target,
961 GrGLenum internalFormat,
brianosman81a84852016-09-12 09:05:14 -0700962 GrGLenum internalFormatForTexStorage,
cblume55f2d2d2016-02-26 13:20:48 -0800963 GrGLenum externalFormat,
964 GrGLenum externalType,
965 const SkTArray<GrMipLevel>& texels,
bsalomone699d0c2016-03-09 06:25:15 -0800966 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800967 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800968
969 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
970 // We can only use TexStorage if we know we will not later change the storage requirements.
971 // This means if we may later want to add mipmaps, we cannot use TexStorage.
972 // Right now, we cannot know if we will later add mipmaps or not.
973 // The only time we can use TexStorage is when we already have the
974 // mipmaps.
975 useTexStorage &= texels.count() > 1;
976
977 if (useTexStorage) {
978 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800979 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -0800980 TexStorage2D(target,
981 texels.count(),
brianosman81a84852016-09-12 09:05:14 -0700982 internalFormatForTexStorage,
cblume790d5132016-02-29 11:13:29 -0800983 desc.fWidth, desc.fHeight));
cblume55f2d2d2016-02-26 13:20:48 -0800984 GrGLenum error = check_alloc_error(desc, &interface);
985 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800986 return false;
cblume790d5132016-02-29 11:13:29 -0800987 } else {
988 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
989 const void* currentMipData = texels[currentMipLevel].fPixels;
990 if (currentMipData == nullptr) {
991 continue;
992 }
993 int twoToTheMipLevel = 1 << currentMipLevel;
994 int currentWidth = SkTMax(1, desc.fWidth / twoToTheMipLevel);
995 int currentHeight = SkTMax(1, desc.fHeight / twoToTheMipLevel);
996
997 GR_GL_CALL(&interface,
998 TexSubImage2D(target,
999 currentMipLevel,
1000 0, // left
1001 0, // top
1002 currentWidth,
1003 currentHeight,
1004 externalFormat, externalType,
1005 currentMipData));
1006 }
bsalomone699d0c2016-03-09 06:25:15 -08001007 return true;
cblume790d5132016-02-29 11:13:29 -08001008 }
1009 } else {
bsalomone699d0c2016-03-09 06:25:15 -08001010 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -08001011 GL_ALLOC_CALL(&interface,
1012 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001013 0,
cblume790d5132016-02-29 11:13:29 -08001014 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001015 baseWidth,
1016 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001017 0, // border
1018 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001019 nullptr));
cblume790d5132016-02-29 11:13:29 -08001020 GrGLenum error = check_alloc_error(desc, &interface);
1021 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001022 return false;
1023 }
1024 } else {
1025 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1026 int twoToTheMipLevel = 1 << currentMipLevel;
1027 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1028 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1029 const void* currentMipData = texels[currentMipLevel].fPixels;
1030 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1031 // This will allocate texture memory which we can later populate.
1032 GL_ALLOC_CALL(&interface,
1033 TexImage2D(target,
1034 currentMipLevel,
1035 internalFormat,
1036 currentWidth,
1037 currentHeight,
1038 0, // border
1039 externalFormat, externalType,
1040 currentMipData));
1041 GrGLenum error = check_alloc_error(desc, &interface);
1042 if (error != GR_GL_NO_ERROR) {
1043 return false;
1044 }
cblume790d5132016-02-29 11:13:29 -08001045 }
cblume55f2d2d2016-02-26 13:20:48 -08001046 }
1047 }
bsalomone699d0c2016-03-09 06:25:15 -08001048 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001049}
1050
1051/**
1052 * Creates storage space for the texture and fills it with texels.
1053 *
1054 * @param desc The surface descriptor for the texture being created.
1055 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -08001056 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -08001057 * @param internalFormat The data format used for the internal storage of the texture.
1058 * @param texels The texel data of the texture being created.
cblume55f2d2d2016-02-26 13:20:48 -08001059 */
1060static bool allocate_and_populate_compressed_texture(const GrSurfaceDesc& desc,
1061 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -08001062 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -08001063 GrGLenum target, GrGLenum internalFormat,
1064 const SkTArray<GrMipLevel>& texels,
1065 int baseWidth, int baseHeight) {
1066 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001067
cblume790d5132016-02-29 11:13:29 -08001068 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
1069 // We can only use TexStorage if we know we will not later change the storage requirements.
1070 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1071 // Right now, we cannot know if we will later add mipmaps or not.
1072 // The only time we can use TexStorage is when we already have the
1073 // mipmaps.
1074 useTexStorage &= texels.count() > 1;
cblume55f2d2d2016-02-26 13:20:48 -08001075
cblume790d5132016-02-29 11:13:29 -08001076 if (useTexStorage) {
1077 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001078 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -08001079 TexStorage2D(target,
1080 texels.count(),
1081 internalFormat,
1082 baseWidth, baseHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001083 GrGLenum error = check_alloc_error(desc, &interface);
1084 if (error != GR_GL_NO_ERROR) {
1085 return false;
cblume790d5132016-02-29 11:13:29 -08001086 } else {
1087 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1088 const void* currentMipData = texels[currentMipLevel].fPixels;
1089 if (currentMipData == nullptr) {
1090 continue;
1091 }
1092
1093 int twoToTheMipLevel = 1 << currentMipLevel;
1094 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1095 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1096
1097 // Make sure that the width and height that we pass to OpenGL
1098 // is a multiple of the block size.
1099 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1100 currentHeight);
1101 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1102 currentMipLevel,
1103 0, // left
1104 0, // top
1105 currentWidth,
1106 currentHeight,
1107 internalFormat,
1108 SkToInt(dataSize),
1109 currentMipData));
1110 }
1111 }
1112 } else {
1113 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1114 int twoToTheMipLevel = 1 << currentMipLevel;
1115 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1116 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1117
1118 // Make sure that the width and height that we pass to OpenGL
1119 // is a multiple of the block size.
1120 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, baseWidth, baseHeight);
1121
1122 GL_ALLOC_CALL(&interface,
1123 CompressedTexImage2D(target,
1124 currentMipLevel,
1125 internalFormat,
1126 currentWidth,
1127 currentHeight,
1128 0, // border
1129 SkToInt(dataSize),
1130 texels[currentMipLevel].fPixels));
1131
1132 GrGLenum error = check_alloc_error(desc, &interface);
1133 if (error != GR_GL_NO_ERROR) {
1134 return false;
1135 }
cblume55f2d2d2016-02-26 13:20:48 -08001136 }
1137 }
1138
1139 return true;
1140}
1141
1142/**
1143 * After a texture is created, any state which was altered during its creation
1144 * needs to be restored.
1145 *
1146 * @param interface The GL interface to use.
1147 * @param caps The capabilities of the GL device.
1148 * @param restoreGLRowLength Should the row length unpacking be restored?
1149 * @param glFlipY Did GL flip the texture vertically?
1150 */
1151static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1152 bool restoreGLRowLength, bool glFlipY) {
1153 if (restoreGLRowLength) {
1154 SkASSERT(caps.unpackRowLengthSupport());
1155 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1156 }
1157 if (glFlipY) {
1158 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1159 }
1160}
1161
bsalomon861e1032014-12-16 07:33:49 -08001162bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -08001163 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -08001164 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001165 int left, int top, int width, int height,
1166 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001167 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001168 // If we're uploading compressed data then we should be using uploadCompressedTexData
1169 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1170
bsalomon5b30c6f2015-12-17 14:17:34 -08001171 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
1172
cblume55f2d2d2016-02-26 13:20:48 -08001173 // texels is const.
1174 // But we may need to flip the texture vertically to prepare it.
1175 // Rather than flip in place and alter the incoming data,
1176 // we allocate a new buffer to flip into.
1177 // This means we need to make a non-const shallow copy of texels.
1178 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1179
1180 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1181 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -08001182 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001183 }
1184
1185 const GrGLInterface* interface = this->glInterface();
1186 const GrGLCaps& caps = this->glCaps();
1187
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001188 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001189
1190 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001191 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001192 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001193
cblume55f2d2d2016-02-26 13:20:48 -08001194 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1195 int twoToTheMipLevel = 1 << currentMipLevel;
1196 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1197 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1198
cblume55f2d2d2016-02-26 13:20:48 -08001199 if (currentHeight > SK_MaxS32 ||
1200 currentWidth > SK_MaxS32) {
1201 return false;
1202 }
1203 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
1204 &currentWidth,
1205 &currentHeight,
1206 &texelsShallowCopy[currentMipLevel].fPixels,
1207 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
1208 return false;
1209 }
1210 if (currentWidth < 0 || currentHeight < 0) {
1211 return false;
1212 }
1213 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001214
bsalomon5b30c6f2015-12-17 14:17:34 -08001215 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001216 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001217 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001218 GrGLenum externalFormat;
1219 GrGLenum externalType;
1220 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
1221 &externalFormat, &externalType)) {
1222 return false;
1223 }
brianosman81a84852016-09-12 09:05:14 -07001224 // TexStorage requires a sized format, and internalFormat may or may not be
1225 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(desc.fConfig);
1226
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001227 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001228 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001229 * because our srcData has extra bytes past each row. If so, we need
1230 * to trim those off here, since GL ES may not let us specify
1231 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001232 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001233 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001234 bool swFlipY = false;
1235 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001236
bsalomone699d0c2016-03-09 06:25:15 -08001237 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001238 if (caps.unpackFlipYSupport()) {
1239 glFlipY = true;
1240 } else {
1241 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001242 }
cblume55f2d2d2016-02-26 13:20:48 -08001243 }
1244
1245 // in case we need a temporary, trimmed copy of the src pixels
1246 SkAutoSMalloc<128 * 128> tempStorage;
1247
1248 // find the combined size of all the mip levels and the relative offset of
1249 // each into the collective buffer
1250 size_t combined_buffer_size = 0;
1251 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1252 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1253 int twoToTheMipLevel = 1 << currentMipLevel;
1254 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1255 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1256 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1257 individual_mip_offsets.push_back(combined_buffer_size);
1258 combined_buffer_size += trimmedSize;
1259 }
1260 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1261
1262 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001263 int twoToTheMipLevel = 1 << currentMipLevel;
1264 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1265 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1266 const size_t trimRowBytes = currentWidth * bpp;
1267
1268 /*
1269 * check whether to allocate a temporary buffer for flipping y or
1270 * because our srcData has extra bytes past each row. If so, we need
1271 * to trim those off here, since GL ES may not let us specify
1272 * GL_UNPACK_ROW_LENGTH.
1273 */
1274 restoreGLRowLength = false;
1275
1276 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
ericrk154349b2016-05-10 14:36:53 -07001277
1278 // TODO: This optimization should be enabled with or without mips.
1279 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1280 // mip level, before calling glTexImage2D.
1281 const bool usesMips = texelsShallowCopy.count() > 1;
1282 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001283 // can't use this for flipping, only non-neg values allowed. :(
1284 if (rowBytes != trimRowBytes) {
1285 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001286 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001287 restoreGLRowLength = true;
1288 }
jvanverth17aa0472016-01-05 10:41:27 -08001289 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001290 if (trimRowBytes != rowBytes || swFlipY) {
1291 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001292 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1293 if (swFlipY && currentHeight >= 1) {
1294 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001295 }
cblume55f2d2d2016-02-26 13:20:48 -08001296 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1297 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001298 memcpy(dst, src, trimRowBytes);
1299 if (swFlipY) {
1300 src -= rowBytes;
1301 } else {
1302 src += rowBytes;
1303 }
1304 dst += trimRowBytes;
1305 }
1306 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001307 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1308 individual_mip_offsets[currentMipLevel];
1309 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001310 }
jvanverth17aa0472016-01-05 10:41:27 -08001311 } else {
1312 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001313 }
bsalomone699d0c2016-03-09 06:25:15 -08001314 }
1315
1316 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001317 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001318 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001319 }
cblume55f2d2d2016-02-26 13:20:48 -08001320 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT,
1321 config_alignment(desc.fConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001322 }
cblume55f2d2d2016-02-26 13:20:48 -08001323
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001324 bool succeeded = true;
cblume55f2d2d2016-02-26 13:20:48 -08001325 if (kNewTexture_UploadType == uploadType &&
1326 0 == left && 0 == top &&
bsalomon7e68ab72016-04-13 14:29:25 -07001327 desc.fWidth == width && desc.fHeight == height) {
bsalomone699d0c2016-03-09 06:25:15 -08001328 succeeded = allocate_and_populate_uncompressed_texture(desc, *interface, caps, target,
brianosman81a84852016-09-12 09:05:14 -07001329 internalFormat,
1330 internalFormatForTexStorage,
1331 externalFormat, externalType,
1332 texelsShallowCopy, width, height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001333 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001334 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001335 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001336 }
cblume55f2d2d2016-02-26 13:20:48 -08001337 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1338 currentMipLevel++) {
1339 int twoToTheMipLevel = 1 << currentMipLevel;
1340 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1341 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001342
1343 GL_CALL(TexSubImage2D(target,
1344 currentMipLevel,
1345 left, top,
1346 currentWidth,
1347 currentHeight,
1348 externalFormat, externalType,
1349 texelsShallowCopy[currentMipLevel].fPixels));
1350 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001351 }
1352
cblume55f2d2d2016-02-26 13:20:48 -08001353 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1354
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001355 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001356}
1357
krajcevski145d48c2014-06-11 16:07:50 -07001358// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001359// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001360// create a CompressedTexData struct that takes a desc/ptr and figures out
1361// the proper upload semantics. Then users can construct this function how they
1362// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -08001363bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -07001364 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -08001365 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -08001366 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001367 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -08001368 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
krajcevski9c0e6292014-06-02 07:38:14 -07001369
1370 // No support for software flip y, yet...
1371 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
1372
cblume55f2d2d2016-02-26 13:20:48 -08001373 const GrGLInterface* interface = this->glInterface();
1374 const GrGLCaps& caps = this->glCaps();
1375
krajcevski145d48c2014-06-11 16:07:50 -07001376 if (-1 == width) {
1377 width = desc.fWidth;
1378 }
1379#ifdef SK_DEBUG
1380 else {
1381 SkASSERT(width <= desc.fWidth);
1382 }
1383#endif
1384
1385 if (-1 == height) {
1386 height = desc.fHeight;
1387 }
1388#ifdef SK_DEBUG
1389 else {
1390 SkASSERT(height <= desc.fHeight);
1391 }
1392#endif
1393
bsalomon76148af2016-01-12 11:13:47 -08001394 // We only need the internal format for compressed 2D textures.
1395 GrGLenum internalFormat;
cblume55f2d2d2016-02-26 13:20:48 -08001396 if (!caps.getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001397 return false;
1398 }
krajcevski9c0e6292014-06-02 07:38:14 -07001399
jvanverth17aa0472016-01-05 10:41:27 -08001400 if (kNewTexture_UploadType == uploadType) {
cblume790d5132016-02-29 11:13:29 -08001401 return allocate_and_populate_compressed_texture(desc, *interface, caps, target,
1402 internalFormat, texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001403 } else {
bsalomond4cb9222014-08-11 14:19:09 -07001404 // Paletted textures can't be updated.
1405 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
1406 return false;
1407 }
cblume55f2d2d2016-02-26 13:20:48 -08001408 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001409 SkASSERT(texels[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001410
1411 int twoToTheMipLevel = 1 << currentMipLevel;
1412 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1413 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1414
1415 // Make sure that the width and height that we pass to OpenGL
1416 // is a multiple of the block size.
1417 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1418 currentHeight);
1419 GL_CALL(CompressedTexSubImage2D(target,
1420 currentMipLevel,
1421 left, top,
1422 currentWidth,
1423 currentHeight,
1424 internalFormat,
1425 SkToInt(dataSize),
1426 texels[currentMipLevel].fPixels));
1427 }
krajcevski145d48c2014-06-11 16:07:50 -07001428 }
krajcevski9c0e6292014-06-02 07:38:14 -07001429
bsalomond4cb9222014-08-11 14:19:09 -07001430 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001431}
1432
bsalomon424cc262015-05-22 10:37:30 -07001433static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001434 int sampleCount,
1435 GrGLenum format,
1436 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001437 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001438 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001439 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001440 case GrGLCaps::kEXT_MSFBOType:
1441 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001442 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001443 GL_ALLOC_CALL(ctx.interface(),
1444 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1445 sampleCount,
1446 format,
1447 width, height));
1448 break;
1449 case GrGLCaps::kES_Apple_MSFBOType:
1450 GL_ALLOC_CALL(ctx.interface(),
1451 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1452 sampleCount,
1453 format,
1454 width, height));
1455 break;
1456 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1457 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1458 GL_ALLOC_CALL(ctx.interface(),
1459 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1460 sampleCount,
1461 format,
1462 width, height));
1463 break;
1464 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001465 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001466 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001467 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001468 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001469}
1470
egdanielb0e1be22015-04-22 13:27:39 -07001471bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001472 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001473 GrGLRenderTarget::IDDesc* idDesc) {
1474 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001475 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001476 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001477 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001478 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1479 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001480 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001481
1482 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001483
bsalomona11e5fc2015-12-18 07:59:41 -08001484 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001485
bsalomonb15b4c12014-10-29 12:41:57 -07001486 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001487 goto FAILED;
1488 }
1489
egdanield803f272015-03-18 13:01:52 -07001490 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1491 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001492 goto FAILED;
1493 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001494
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001495 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1496 // the texture bound to the other. The exception is the IMG multisample extension. With this
1497 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1498 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001499 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001500 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001501 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001502 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001503 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001504 goto FAILED;
1505 }
bsalomon76148af2016-01-12 11:13:47 -08001506 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1507 return false;
1508 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001509 } else {
egdanield803f272015-03-18 13:01:52 -07001510 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 }
1512
egdanield803f272015-03-18 13:01:52 -07001513 // below here we may bind the FBO
1514 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1515 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001516 SkASSERT(desc.fSampleCnt > 0);
1517 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001518 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001519 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001520 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001521 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001522 goto FAILED;
1523 }
egdanield803f272015-03-18 13:01:52 -07001524 fStats.incRenderTargetBinds();
1525 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1526 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001527 GR_GL_COLOR_ATTACHMENT0,
1528 GR_GL_RENDERBUFFER,
1529 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001530 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1531 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001532 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1533 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1534 goto FAILED;
1535 }
bsalomon424cc262015-05-22 10:37:30 -07001536 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001537 }
1538 }
egdanield803f272015-03-18 13:01:52 -07001539 fStats.incRenderTargetBinds();
1540 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001541
bsalomonb15b4c12014-10-29 12:41:57 -07001542 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001543 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001544 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001545 texInfo.fTarget,
1546 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001547 } else {
egdanield803f272015-03-18 13:01:52 -07001548 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001549 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001550 texInfo.fTarget,
1551 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001552 }
bsalomonb15b4c12014-10-29 12:41:57 -07001553 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1554 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001555 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001556 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1557 goto FAILED;
1558 }
bsalomon424cc262015-05-22 10:37:30 -07001559 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001560 }
1561
1562 return true;
1563
1564FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001565 if (idDesc->fMSColorRenderbufferID) {
1566 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001567 }
egdanield803f272015-03-18 13:01:52 -07001568 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1569 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001570 }
egdanield803f272015-03-18 13:01:52 -07001571 if (idDesc->fTexFBOID) {
1572 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001573 }
1574 return false;
1575}
1576
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001577// good to set a break-point here to know when createTexture fails
1578static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001579// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001580 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001581}
1582
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001583#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001584static size_t as_size_t(int x) {
1585 return x;
1586}
1587#endif
1588
kkinnunen2e6055b2016-04-22 01:48:29 -07001589static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface) {
cblume55f2d2d2016-02-26 13:20:48 -08001590 GrGLTexture::IDDesc idDesc;
1591 idDesc.fInfo.fID = 0;
1592 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
kkinnunen2e6055b2016-04-22 01:48:29 -07001593 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
cblume55f2d2d2016-02-26 13:20:48 -08001594 // When we create the texture, we only
1595 // create GL_TEXTURE_2D at the moment.
1596 // External clients can do something different.
1597 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1598 return idDesc;
1599}
1600
1601static void set_initial_texture_params(const GrGLInterface* interface,
1602 const GrGLTextureInfo& info,
1603 GrGLTexture::TexParams* initialTexParams) {
1604 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1605 // drivers have a bug where an FBO won't be complete if it includes a
1606 // texture that is not mipmap complete (considering the filter in use).
1607 // we only set a subset here so invalidate first
1608 initialTexParams->invalidate();
1609 initialTexParams->fMinFilter = GR_GL_NEAREST;
1610 initialTexParams->fMagFilter = GR_GL_NEAREST;
1611 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1612 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1613 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1614 GR_GL_TEXTURE_MAG_FILTER,
1615 initialTexParams->fMagFilter));
1616 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1617 GR_GL_TEXTURE_MIN_FILTER,
1618 initialTexParams->fMinFilter));
1619 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1620 GR_GL_TEXTURE_WRAP_S,
1621 initialTexParams->fWrapS));
1622 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1623 GR_GL_TEXTURE_WRAP_T,
1624 initialTexParams->fWrapT));
1625}
1626
egdanielb0e1be22015-04-22 13:27:39 -07001627GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001628 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001629 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001630 // We fail if the MSAA was requested and is not available.
1631 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001632 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001633 return return_null_texture();
1634 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001635
bsalomonf2703d82014-10-28 14:33:06 -07001636 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001637
bsalomonb15b4c12014-10-29 12:41:57 -07001638 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001639 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001640 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001641 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001642 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001643 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001644
Robert Phillipsd6214d42016-11-07 08:23:48 -05001645 bool wasMipMapDataProvided = false;
1646 if (texels.count() > 1) {
1647 wasMipMapDataProvided = true;
1648 }
1649
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001650 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001651 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001652 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001653 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001654 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001655
kkinnunen2e6055b2016-04-22 01:48:29 -07001656 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001657 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 return return_null_texture();
1659 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001660 tex = new GrGLTextureRenderTarget(this, budgeted, desc, idDesc, rtIDDesc,
1661 wasMipMapDataProvided);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001662 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -07001663 tex = new GrGLTexture(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001664 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001665 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001666#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001667 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001668 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001669#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001670 return tex;
1671}
1672
egdanielb0e1be22015-04-22 13:27:39 -07001673GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001674 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001675 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001676 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001677 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001678 return return_null_texture();
1679 }
1680
kkinnunen2e6055b2016-04-22 01:48:29 -07001681 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface());
bsalomon091f60c2015-11-10 11:54:56 -08001682 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001683 return return_null_texture();
1684 }
1685
1686 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001687 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001688
krajcevski9c0e6292014-06-02 07:38:14 -07001689 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001690 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001691
cblume55f2d2d2016-02-26 13:20:48 -08001692 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001693 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001694 return return_null_texture();
1695 }
1696
1697 GrGLTexture* tex;
kkinnunen2e6055b2016-04-22 01:48:29 -07001698 tex = new GrGLTexture(this, budgeted, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001699 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1700#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001701 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001702 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001703#endif
1704 return tex;
1705}
1706
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001707namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001708
egdaniel8dc7c3a2015-04-16 11:22:42 -07001709const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001710
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001711void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001712 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001713
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001714 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001715 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001716 (kUnknownBitCount == format->fTotalBits));
1717 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001718 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001719 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1720 (GrGLint*)&format->fStencilBits);
1721 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001722 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001723 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1724 (GrGLint*)&format->fTotalBits);
1725 format->fTotalBits += format->fStencilBits;
1726 } else {
1727 format->fTotalBits = format->fStencilBits;
1728 }
1729 }
1730}
1731}
1732
egdanielff1d5472015-09-10 08:37:20 -07001733int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001734 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001735 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001736 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1737 // Default to unsupported, set this if we find a stencil format that works.
1738 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001739 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001740 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001741 GL_CALL(GenTextures(1, &colorID));
1742 this->setScratchTextureUnit();
1743 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1744 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1745 GR_GL_TEXTURE_MAG_FILTER,
1746 GR_GL_NEAREST));
1747 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1748 GR_GL_TEXTURE_MIN_FILTER,
1749 GR_GL_NEAREST));
1750 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1751 GR_GL_TEXTURE_WRAP_S,
1752 GR_GL_CLAMP_TO_EDGE));
1753 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1754 GR_GL_TEXTURE_WRAP_T,
1755 GR_GL_CLAMP_TO_EDGE));
1756
bsalomon76148af2016-01-12 11:13:47 -08001757 GrGLenum internalFormat;
1758 GrGLenum externalFormat;
1759 GrGLenum externalType;
1760 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1761 &externalType)) {
1762 return false;
1763 }
egdanielff1d5472015-09-10 08:37:20 -07001764 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1765 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001766 0,
bsalomon76148af2016-01-12 11:13:47 -08001767 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001768 kSize,
1769 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001770 0,
bsalomon76148af2016-01-12 11:13:47 -08001771 externalFormat,
1772 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001773 NULL));
bsalomon30447372015-12-21 09:03:05 -08001774 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001775 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001776 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001777 }
1778
1779 // unbind the texture from the texture unit before binding it to the frame buffer
1780 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1781
1782 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001783 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001784 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001785 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001786 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1787 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1788 GR_GL_COLOR_ATTACHMENT0,
1789 GR_GL_TEXTURE_2D,
1790 colorID,
1791 0));
bsalomon30447372015-12-21 09:03:05 -08001792 GrGLuint sbRBID = 0;
1793 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001794
1795 // look over formats till I find a compatible one
1796 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001797 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001798 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001799 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1800 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1801 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1802 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1803 sFmt.fInternalFormat,
1804 kSize, kSize));
1805 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001806 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001807 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001808 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001809 if (sFmt.fPacked) {
1810 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1811 GR_GL_DEPTH_ATTACHMENT,
1812 GR_GL_RENDERBUFFER, sbRBID));
1813 } else {
1814 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1815 GR_GL_DEPTH_ATTACHMENT,
1816 GR_GL_RENDERBUFFER, 0));
1817 }
1818 GrGLenum status;
1819 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1820 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1821 firstWorkingStencilFormatIndex = i;
1822 break;
1823 }
egdanielff1d5472015-09-10 08:37:20 -07001824 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1825 GR_GL_STENCIL_ATTACHMENT,
1826 GR_GL_RENDERBUFFER, 0));
1827 if (sFmt.fPacked) {
1828 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1829 GR_GL_DEPTH_ATTACHMENT,
1830 GR_GL_RENDERBUFFER, 0));
1831 }
egdanielff1d5472015-09-10 08:37:20 -07001832 }
1833 }
bsalomon30447372015-12-21 09:03:05 -08001834 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001835 }
1836 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001837 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1838 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001839 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001840 }
bsalomon30447372015-12-21 09:03:05 -08001841 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001842}
1843
erikchen9a1ed5d2016-02-10 16:32:34 -08001844bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001845 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1846 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001847 info->fID = 0;
1848 info->fTarget = GR_GL_TEXTURE_2D;
1849 GL_CALL(GenTextures(1, &(info->fID)));
1850
1851 if (!info->fID) {
1852 return false;
1853 }
1854
1855 this->setScratchTextureUnit();
1856 GL_CALL(BindTexture(info->fTarget, info->fID));
1857
1858 if (renderTarget && this->glCaps().textureUsageSupport()) {
1859 // provides a hint about how this texture will be used
1860 GL_CALL(TexParameteri(info->fTarget,
1861 GR_GL_TEXTURE_USAGE,
1862 GR_GL_FRAMEBUFFER_ATTACHMENT));
1863 }
1864
cblume55f2d2d2016-02-26 13:20:48 -08001865 if (info) {
1866 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1867 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001868 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1869 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001870 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001871 GL_CALL(DeleteTextures(1, &(info->fID)));
1872 return false;
1873 }
1874 return true;
1875}
1876
egdanielec00d942015-09-14 12:56:10 -07001877GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1878 int width,
1879 int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001880 SkASSERT(width >= rt->width());
1881 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001882
vbuzinovdded6962015-06-12 08:59:45 -07001883 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001884 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001885
egdanielff1d5472015-09-10 08:37:20 -07001886 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001887 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001888 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001889 }
egdanielff1d5472015-09-10 08:37:20 -07001890
1891 if (!sbDesc.fRenderbufferID) {
1892 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1893 }
1894 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001895 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001896 }
1897 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1898 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1899 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1900 // we do this "if" so that we don't call the multisample
1901 // version on a GL that doesn't have an MSAA extension.
1902 if (samples > 0) {
1903 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1904 samples,
1905 sFmt.fInternalFormat,
1906 width, height));
1907 } else {
1908 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1909 sFmt.fInternalFormat,
1910 width, height));
1911 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1912 }
1913 fStats.incStencilAttachmentCreates();
1914 // After sized formats we attempt an unsized format and take
1915 // whatever sizes GL gives us. In that case we query for the size.
1916 GrGLStencilAttachment::Format format = sFmt;
1917 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001918 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1919 sbDesc,
1920 width,
1921 height,
1922 samples,
1923 format);
1924 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001925}
1926
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001927////////////////////////////////////////////////////////////////////////////////
1928
jvanverth73063dc2015-12-03 09:15:47 -08001929// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1930// objects are implemented as client-side-arrays on tile-deferred architectures.
1931#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1932
cdaltone2e71c22016-04-07 18:13:29 -07001933GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001934 GrAccessPattern accessPattern, const void* data) {
1935 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001936}
1937
csmartdaltone0d36292016-07-29 08:14:20 -07001938InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1939 return new GLInstancedRendering(this);
1940}
1941
bsalomon3e791242014-12-17 13:43:13 -08001942void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001943 const GrGLIRect& rtViewport,
1944 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001945 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001946 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001947 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001948 scissorState.rect().fLeft,
1949 scissorState.rect().fTop,
1950 scissorState.rect().width(),
1951 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001952 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001953 // if the scissor fully contains the viewport then we fall through and
1954 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001955 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001956 if (fHWScissorSettings.fRect != scissor) {
1957 scissor.pushToGLScissor(this->glInterface());
1958 fHWScissorSettings.fRect = scissor;
1959 }
1960 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1961 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1962 fHWScissorSettings.fEnabled = kYes_TriState;
1963 }
1964 return;
1965 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001966 }
joshualitt77b13072014-10-27 14:51:01 -07001967
1968 // See fall through note above
1969 this->disableScissor();
1970}
1971
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001972void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
1973 const GrGLRenderTarget* rt) {
1974 typedef GrWindowRectsState::Mode Mode;
1975 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1976 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001977
1978 if (!this->caps()->maxWindowRectangles() ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001979 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001980 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001981 }
1982
csmartdalton7535f412016-08-23 06:51:00 -07001983 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1984 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001985 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1986 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001987
csmartdalton28341fa2016-08-17 10:00:21 -07001988 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001989 const SkIRect* skwindows = windowState.windows().data();
1990 int dx = -windowState.origin().x(), dy = -windowState.origin().y();
csmartdalton7535f412016-08-23 06:51:00 -07001991 for (int i = 0; i < numWindows; ++i) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001992 const SkIRect& skwindow = skwindows[i].makeOffset(dx, dy);
1993 glwindows[i].setRelativeTo(rt->getViewport(), skwindow, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07001994 }
1995
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001996 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001997 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001998
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001999 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
csmartdalton28341fa2016-08-17 10:00:21 -07002000}
2001
2002void GrGLGpu::disableWindowRectangles() {
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002003 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07002004 return;
2005 }
2006 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002007 fHWWindowRectsState.setDisabled();
csmartdalton28341fa2016-08-17 10:00:21 -07002008}
2009
ethannicholas28ef4452016-03-25 09:26:03 -07002010void GrGLGpu::flushMinSampleShading(float minSampleShading) {
2011 if (fHWMinSampleShading != minSampleShading) {
2012 if (minSampleShading > 0.0) {
2013 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
2014 GL_CALL(MinSampleShading(minSampleShading));
2015 }
2016 else {
2017 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
2018 }
2019 fHWMinSampleShading = minSampleShading;
2020 }
2021}
2022
bsalomon2eda5b32016-09-21 10:53:24 -07002023bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
2024 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05002025 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07002026 if (!program) {
2027 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2028 return false;
2029 }
2030
2031 program->generateMipmaps(primProc, pipeline);
2032
egdaniel080e6732014-12-22 07:35:52 -08002033 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002034 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002035
egdaniel080e6732014-12-22 07:35:52 -08002036 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08002037 this->flushDrawFace(pipeline.getDrawFace());
ethannicholas28ef4452016-03-25 09:26:03 -07002038 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08002039
bsalomon6df86402015-06-01 10:41:49 -07002040 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08002041 if (fHWProgramID != programID) {
2042 GL_CALL(UseProgram(programID));
2043 fHWProgramID = programID;
2044 }
2045
egdanield803f272015-03-18 13:01:52 -07002046 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08002047 // Swizzle the blend to match what the shader will output.
2048 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07002049 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08002050 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07002051 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002052
cdalton74b8d322016-04-11 14:47:28 -07002053 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08002054
brianosman898235c2016-04-06 07:38:23 -07002055 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07002056 GrStencilSettings stencil;
2057 if (pipeline.isStencilEnabled()) {
2058 // TODO: attach stencil and create settings during render target flush.
2059 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
2060 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
2061 glRT->renderTargetPriv().numStencilBits());
2062 }
2063 this->flushStencil(stencil);
egdaniel8dd688b2015-01-22 10:16:09 -08002064 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002065 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
csmartdaltonc633abb2016-11-01 08:55:55 -07002066 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08002067
2068 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002069 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07002070 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08002071
2072 return true;
2073}
2074
joshualitt873ad0e2015-01-20 09:08:51 -08002075void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
egdaniel0e1853c2016-03-17 11:35:45 -07002076 const GrNonInstancedMesh& mesh,
bsalomonbc3d0de2014-12-15 13:45:03 -08002077 size_t* indexOffsetInBytes) {
csmartdalton485a1202016-07-13 10:16:32 -07002078 const GrBuffer* vbuf = mesh.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08002079 SkASSERT(vbuf);
2080 SkASSERT(!vbuf->isMapped());
2081
cdaltone2e71c22016-04-07 18:13:29 -07002082 GrGLAttribArrayState* attribState;
egdaniel0e1853c2016-03-17 11:35:45 -07002083 if (mesh.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002084 SkASSERT(indexOffsetInBytes);
2085
2086 *indexOffsetInBytes = 0;
csmartdalton485a1202016-07-13 10:16:32 -07002087 const GrBuffer* ibuf = mesh.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08002088 SkASSERT(ibuf);
2089 SkASSERT(!ibuf->isMapped());
2090 *indexOffsetInBytes += ibuf->baseOffset();
cdaltone2e71c22016-04-07 18:13:29 -07002091 attribState = fHWVertexArrayState.bindInternalVertexArray(this, ibuf);
2092 } else {
2093 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002094 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002095
joshualitt873ad0e2015-01-20 09:08:51 -08002096 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08002097 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002098
joshualitt873ad0e2015-01-20 09:08:51 -08002099 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08002100
egdaniel0e1853c2016-03-17 11:35:45 -07002101 size_t vertexOffsetInBytes = stride * mesh.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08002102
2103 vertexOffsetInBytes += vbuf->baseOffset();
2104
bsalomonbc3d0de2014-12-15 13:45:03 -08002105 uint32_t usedAttribArraysMask = 0;
2106 size_t offset = 0;
2107
2108 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08002109 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08002110 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08002111 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08002112 attribState->set(this,
2113 attribIndex,
cdaltone2e71c22016-04-07 18:13:29 -07002114 vbuf,
cdalton793dc262016-02-08 10:11:47 -08002115 attribType,
bsalomonbc3d0de2014-12-15 13:45:03 -08002116 stride,
2117 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08002118 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002119 }
2120 attribState->disableUnusedArrays(this, usedAttribArraysMask);
2121 }
2122}
2123
csmartdalton485a1202016-07-13 10:16:32 -07002124GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002125 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002126
cdaltone2e71c22016-04-07 18:13:29 -07002127 // Index buffer state is tied to the vertex array.
2128 if (kIndex_GrBufferType == type) {
2129 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002130 }
cdaltone2e71c22016-04-07 18:13:29 -07002131
2132 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2133 auto& bufferState = fHWBufferState[type];
2134
robertphillips8abb3702016-08-31 14:04:06 -07002135 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07002136 if (buffer->isCPUBacked()) {
2137 if (!bufferState.fBufferZeroKnownBound) {
2138 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
2139 }
2140 } else {
2141 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2142 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07002143 }
csmartdalton485a1202016-07-13 10:16:32 -07002144 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07002145 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002146 }
2147
2148 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002149}
2150
cdalton74b8d322016-04-11 14:47:28 -07002151void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
2152 if (buffer->hasAttachedToTexture()) {
2153 // Detach this buffer from any textures to ensure the underlying memory is freed.
robertphillips8abb3702016-08-31 14:04:06 -07002154 uint32_t uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07002155 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
2156 auto& buffTex = fHWBufferTextures[i];
2157 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
2158 continue;
2159 }
2160 if (i == fHWMaxUsedBufferTextureUnit) {
2161 --fHWMaxUsedBufferTextureUnit;
2162 }
2163
2164 this->setTextureUnit(i);
2165 if (!buffTex.fKnownBound) {
2166 SkASSERT(buffTex.fTextureID);
2167 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
2168 buffTex.fKnownBound = true;
2169 }
2170 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
2171 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
2172 }
2173 }
2174}
2175
bsalomon861e1032014-12-16 07:33:49 -08002176void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002177 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002178 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002179 fHWScissorSettings.fEnabled = kNo_TriState;
2180 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 }
2182}
2183
csmartdalton29df7602016-08-31 11:55:52 -07002184void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07002185 this->handleDirtyContext();
2186
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002187 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002188 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002189 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002190
csmartdalton29df7602016-08-31 11:55:52 -07002191 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
2192 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002193 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00002194
2195 GrGLfloat r, g, b, a;
2196 static const GrGLfloat scale255 = 1.f / 255.f;
2197 a = GrColorUnpackA(color) * scale255;
2198 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002199 r = GrColorUnpackR(color) * scaleRGB;
2200 g = GrColorUnpackG(color) * scaleRGB;
2201 b = GrColorUnpackB(color) * scaleRGB;
2202
2203 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002204 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002205 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002207}
2208
bsalomon861e1032014-12-16 07:33:49 -08002209void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002210 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002211 return;
2212 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002213 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002214 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002215
joshualitt77b13072014-10-27 14:51:01 -07002216 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002217 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002218
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(StencilMask(0xffffffff));
2220 GL_CALL(ClearStencil(0));
2221 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002222 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002223}
2224
csmartdalton29df7602016-08-31 11:55:52 -07002225void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2226 bool insideStencilMask,
2227 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07002228 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002229 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002230
egdaniel8dc7c3a2015-04-16 11:22:42 -07002231 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002232 // this should only be called internally when we know we have a
2233 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002234 SkASSERT(sb);
2235 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002236#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002237 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002238 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002239#else
2240 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002241 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002242 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002243 // changing the clip between stencil passes may or may not
2244 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002245 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002246#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002247 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002248 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002249 value = (1 << (stencilBitCount - 1));
2250 } else {
2251 value = 0;
2252 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002253 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002254 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002255
csmartdalton29df7602016-08-31 11:55:52 -07002256 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002257 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002258
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002259 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002260 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002261 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002262 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002263}
2264
Brian Salomon71d9d842016-11-03 13:42:00 -04002265static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002266 int width, int height, GrPixelConfig config,
2267 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002268 // If the surface is already TopLeft, we don't need to flip.
2269 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002270 return false;
2271 }
2272
bsalomon494aa592015-07-23 11:45:02 -07002273 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002274 static const int kMinSize = 32;
2275 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002276 return false;
2277 }
2278
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002279 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002280 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002281 return false;
2282 }
2283
2284 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002285 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002286 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2287 // clipped.
2288 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2289}
2290
bsalomon1aa20292016-01-22 08:16:09 -08002291bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
2292 auto bindRenderTarget = [this, target]() -> bool {
2293 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2294 return true;
2295 };
bsalomon2c3db322016-11-08 13:26:24 -08002296 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002297 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2298 GR_GL_GetIntegerv(this->glInterface(), query, value);
2299 };
2300 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002301 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2302 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002303}
2304
2305bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002306 sk_sp<GrTexture> temp;
2307 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
bsalomon1aa20292016-01-22 08:16:09 -08002308 GrTextureDesc desc;
2309 desc.fConfig = rtConfig;
2310 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002311 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2312 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2313 temp.reset(this->createTexture(desc, SkBudgeted::kNo));
2314 if (!temp) {
2315 return false;
2316 }
2317 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2318 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2319 return true;
2320 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
2321 temp.reset(this->createTexture(desc, SkBudgeted::kNo));
2322 if (!temp) {
2323 return false;
2324 }
2325 GrGLIRect vp;
2326 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
2327 fHWBoundRenderTargetUniqueID = 0;
2328 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002329 }
bsalomon2c3db322016-11-08 13:26:24 -08002330 return false;
2331 };
2332 auto unbindRenderTarget = [this, &temp]() {
2333 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002334 };
2335 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2336 GR_GL_GetIntegerv(this->glInterface(), query, value);
2337 };
bsalomon2c3db322016-11-08 13:26:24 -08002338 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2339 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002340}
2341
2342bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2343 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2344 return this->readPixelsSupported(rt, readConfig);
2345 } else {
2346 GrPixelConfig config = surfaceForConfig->config();
2347 return this->readPixelsSupported(config, readConfig);
2348 }
2349}
2350
bsalomone9573312016-01-25 14:33:25 -08002351static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2352 if (GrPixelConfigIsSRGB(a)) {
2353 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2354 } else if (GrPixelConfigIsSRGB(b)) {
2355 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2356 }
2357 return false;
2358}
2359
bsalomonf0674512015-07-28 13:26:15 -07002360bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2361 GrPixelConfig readConfig, DrawPreference* drawPreference,
2362 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002363 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002364
bsalomone9573312016-01-25 14:33:25 -08002365 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002366 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2367 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2368 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2369 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2370 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002371 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2372 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002373 // For now assume no swizzling, we may change that below.
2374 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2375
2376 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2377 // from will be srcConfig and we will read readConfig pixels from it.
2378 // Not that if we require a draw and return a non-renderable format for the temp surface the
2379 // base class will fail for us.
2380 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2381 tempDrawInfo->fReadConfig = readConfig;
2382
2383 if (requires_srgb_conversion(srcConfig, readConfig)) {
2384 if (!this->readPixelsSupported(readConfig, readConfig)) {
2385 return false;
2386 }
2387 // Draw to do srgb to linear conversion or vice versa.
2388 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2389 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2390 tempDrawInfo->fReadConfig = readConfig;
2391 return true;
2392 }
2393
bsalomon1aa20292016-01-22 08:16:09 -08002394 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2395 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002396 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002397 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2398 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002399 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002400 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002401 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002402 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2403 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002404 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002405 // Better to do a draw with a R/B swap and then read as the original config.
2406 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002407 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2408 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002409 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002410 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2411 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002412 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002413 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002414 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2415 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2416 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002417 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2418 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2419 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2420 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002421 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002422 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002423 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2424 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2425 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2426 // will effectively be sBGRA).
2427 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2428 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2429 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2430 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002431 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2432 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2433 // it's unsupported, but 32bit RGBA reads are supported.
2434 // Don't attempt to do any srgb conversions since we only care about alpha.
2435 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2436 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2437 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2438 }
2439 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2440 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2441 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002442 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002443 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2444 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2445 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2446 } else {
2447 return false;
2448 }
2449 } else {
2450 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2451 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2452 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002453 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002454 this->readPixelsSupported(readConfig, readConfig)) {
2455 // Do a draw to convert from the src config to the read config.
2456 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2457 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2458 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002459 } else {
2460 return false;
2461 }
bsalomon39826022015-07-23 08:07:21 -07002462 }
2463
Brian Salomon71d9d842016-11-03 13:42:00 -04002464 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
2465 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
2466 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002467 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002468 }
2469
bsalomon39826022015-07-23 08:07:21 -07002470 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002471}
2472
bsalomon6cb3cbe2015-07-30 07:34:27 -07002473bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002474 int left, int top,
2475 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002476 GrPixelConfig config,
2477 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002478 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002479 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002480
bsalomone9573312016-01-25 14:33:25 -08002481 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002482 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002483 return false;
2484 }
2485
bsalomon16921ec2015-07-30 15:34:56 -07002486 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002487 if (requires_srgb_conversion(surface->config(), config)) {
2488 return false;
2489 }
2490
2491 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2492 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002493 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002494 // Don't attempt to do any srgb conversions since we only care about alpha.
2495 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002496 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002497 tempConfig = kSRGBA_8888_GrPixelConfig;
2498 }
2499 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002500 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002501 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Brian Salomon71d9d842016-11-03 13:42:00 -04002502 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
bsalomone9573312016-01-25 14:33:25 -08002503 width*4)) {
2504 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2505 for (int j = 0; j < height; ++j) {
2506 for (int i = 0; i < width; ++i) {
2507 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2508 }
2509 }
2510 return true;
2511 }
2512 }
bsalomon16921ec2015-07-30 15:34:56 -07002513 return false;
2514 }
2515
bsalomon76148af2016-01-12 11:13:47 -08002516 GrGLenum externalFormat;
2517 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002518 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002519 &externalType)) {
2520 return false;
2521 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002522 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002523
Brian Salomon71d9d842016-11-03 13:42:00 -04002524 GrGLIRect glvp;
2525 if (renderTarget) {
2526 // resolve the render target if necessary
2527 switch (renderTarget->getResolveType()) {
2528 case GrGLRenderTarget::kCantResolve_ResolveType:
2529 return false;
2530 case GrGLRenderTarget::kAutoResolves_ResolveType:
2531 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2532 break;
2533 case GrGLRenderTarget::kCanResolve_ResolveType:
2534 this->onResolveRenderTarget(renderTarget);
2535 // we don't track the state of the READ FBO ID.
2536 fStats.incRenderTargetBinds();
2537 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2538 break;
2539 default:
2540 SkFAIL("Unknown resolve type");
2541 }
2542 glvp = renderTarget->getViewport();
2543 } else {
2544 // Use a temporary FBO.
2545 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
2546 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
reed@google.comac10a2d2010-12-22 21:39:39 +00002547 }
2548
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002549 // the read rect is viewport-relative
2550 GrGLIRect readRect;
Brian Salomon71d9d842016-11-03 13:42:00 -04002551 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002552
bsalomon9d02b262016-02-01 12:49:30 -08002553 size_t bytesPerPixel = GrBytesPerPixel(config);
2554 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002555
bsalomon@google.comc6980972011-11-02 19:57:21 +00002556 size_t readDstRowBytes = tightRowBytes;
2557 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002558
bsalomon@google.comc6980972011-11-02 19:57:21 +00002559 // determine if GL can read using the passed rowBytes or if we need
2560 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002561 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002562 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002563 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002564 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002565 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002566 readDstRowBytes = rowBytes;
2567 } else {
2568 scratch.reset(tightRowBytes * height);
2569 readDst = scratch.get();
2570 }
2571 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002572 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002573 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2574 }
bsalomonf46a1242015-12-15 12:37:38 -08002575 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2576
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002577 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2578 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002579 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002580 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002581 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002582 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2583 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002584 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002585 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002586 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002587 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002588
2589 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002590 // API presents top-to-bottom. We must preserve the padding contents. Note
2591 // that the above readPixels did not overwrite the padding.
2592 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002593 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002594 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002595 scratch.reset(tightRowBytes);
2596 void* tmpRow = scratch.get();
2597 // flip y in-place by rows
2598 const int halfY = height >> 1;
2599 char* top = reinterpret_cast<char*>(buffer);
2600 char* bottom = top + (height - 1) * rowBytes;
2601 for (int y = 0; y < halfY; y++) {
2602 memcpy(tmpRow, top, tightRowBytes);
2603 memcpy(top, bottom, tightRowBytes);
2604 memcpy(bottom, tmpRow, tightRowBytes);
2605 top += rowBytes;
2606 bottom -= rowBytes;
2607 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002608 }
2609 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002610 SkASSERT(readDst != buffer);
2611 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002612 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002613 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002614 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002615 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002616 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002617 dst += (height-1) * rowBytes;
2618 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002619 for (int y = 0; y < height; y++) {
2620 memcpy(dst, src, tightRowBytes);
2621 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002622 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002623 dst += rowBytes;
2624 } else {
2625 dst -= rowBytes;
2626 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002627 }
2628 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002629 if (!renderTarget) {
2630 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2631 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002632 return true;
2633}
2634
egdaniel9cb63402016-06-23 08:37:05 -07002635GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
2636 GrRenderTarget* target,
2637 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2638 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Greg Daniel65a09272016-10-12 09:47:22 -04002639 return new GrGLGpuCommandBuffer(this, static_cast<GrGLRenderTarget*>(target));
egdaniel066df7c2016-06-08 14:02:27 -07002640}
2641
Robert Phillipsf2361d22016-10-25 14:20:06 -04002642void GrGLGpu::finishOpList() {
ethannicholas22793252016-01-30 09:59:10 -08002643 if (fPLSHasBeenUsed) {
halcanary9d524f22016-03-29 09:03:52 -07002644 /* There is an ARM driver bug where if we use PLS, and then draw a frame which does not
2645 * use PLS, it leaves garbage all over the place. As a workaround, we use PLS in a
ethannicholas22793252016-01-30 09:59:10 -08002646 * trivial way every frame. And since we use it every frame, there's never a point at which
2647 * it becomes safe to stop using this workaround once we start.
2648 */
2649 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002650 this->disableWindowRectangles();
ethannicholas22793252016-01-30 09:59:10 -08002651 // using PLS in the presence of MSAA results in GL_INVALID_OPERATION
cdaltonaf8bc7d2016-02-05 09:35:20 -08002652 this->flushHWAAState(nullptr, false, false);
ethannicholas22793252016-01-30 09:59:10 -08002653 SkASSERT(!fHWPLSEnabled);
2654 SkASSERT(fMSAAEnabled != kYes_TriState);
2655 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
cdaltone2e71c22016-04-07 18:13:29 -07002656 this->stampPLSSetupRect(SkRect::MakeXYWH(-100.0f, -100.0f, 0.01f, 0.01f));
ethannicholas22793252016-01-30 09:59:10 -08002657 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2658 }
2659}
ethannicholas5366a092016-01-22 09:45:47 -08002660
brianosman64d094d2016-03-25 06:01:59 -07002661void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002662 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002663
robertphillips8abb3702016-08-31 14:04:06 -07002664 uint32_t rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002665 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002666 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002667 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2668#ifdef SK_DEBUG
2669 // don't do this check in Chromium -- this is causing
2670 // lots of repeated command buffer flushes when the compositor is
2671 // rendering with Ganesh, which is really slow; even too slow for
2672 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002673 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002674 GrGLenum status;
2675 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2676 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2677 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2678 }
bsalomon160f24c2015-03-17 15:55:42 -07002679 }
egdanield803f272015-03-18 13:01:52 -07002680#endif
2681 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002682 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002683 }
2684
brianosman35b784d2016-05-05 11:52:53 -07002685 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002686 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002687 }
brianosman64d094d2016-03-25 06:01:59 -07002688
bsalomon083617b2016-02-12 12:10:14 -08002689 this->didWriteToSurface(target, bounds);
2690}
bsalomona9909122016-01-23 10:41:40 -08002691
brianosman33f6b3f2016-06-02 05:49:21 -07002692void GrGLGpu::flushFramebufferSRGB(bool enable) {
2693 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2694 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2695 fHWSRGBFramebuffer = kYes_TriState;
2696 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2697 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2698 fHWSRGBFramebuffer = kNo_TriState;
2699 }
2700}
2701
bsalomon083617b2016-02-12 12:10:14 -08002702void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2703 if (fHWViewport != viewport) {
2704 viewport.pushToGLViewport(this->glInterface());
2705 fHWViewport = viewport;
2706 }
2707}
2708
twiz@google.com0f31ca72011-03-18 17:38:11 +00002709GrGLenum gPrimitiveType2GLMode[] = {
2710 GR_GL_TRIANGLES,
2711 GR_GL_TRIANGLE_STRIP,
2712 GR_GL_TRIANGLE_FAN,
2713 GR_GL_POINTS,
2714 GR_GL_LINES,
2715 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002716};
2717
bsalomon@google.comd302f142011-03-03 13:54:13 +00002718#define SWAP_PER_DRAW 0
2719
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002720#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002721 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002722 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002723 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002724 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002725 void SwapBuf() {
2726 DWORD procID = GetCurrentProcessId();
2727 HWND hwnd = GetTopWindow(GetDesktopWindow());
2728 while(hwnd) {
2729 DWORD wndProcID = 0;
2730 GetWindowThreadProcessId(hwnd, &wndProcID);
2731 if(wndProcID == procID) {
2732 SwapBuffers(GetDC(hwnd));
2733 }
2734 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2735 }
2736 }
2737 #endif
2738#endif
2739
egdaniel9cb63402016-06-23 08:37:05 -07002740void GrGLGpu::draw(const GrPipeline& pipeline,
2741 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002742 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002743 int meshCount) {
2744 this->handleDirtyContext();
2745
bsalomon2eda5b32016-09-21 10:53:24 -07002746 bool hasPoints = false;
2747 for (int i = 0; i < meshCount; ++i) {
2748 if (meshes[i].primitiveType() == kPoints_GrPrimitiveType) {
2749 hasPoints = true;
2750 break;
2751 }
2752 }
2753 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002754 return;
2755 }
egdaniel0e1853c2016-03-17 11:35:45 -07002756 GrPixelLocalStorageState plsState = primProc.getPixelLocalStorageState();
2757 if (!fHWPLSEnabled && plsState !=
ethannicholas22793252016-01-30 09:59:10 -08002758 GrPixelLocalStorageState::kDisabled_GrPixelLocalStorageState) {
2759 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
egdaniel0e1853c2016-03-17 11:35:45 -07002760 this->setupPixelLocalStorage(pipeline, primProc);
ethannicholas22793252016-01-30 09:59:10 -08002761 fHWPLSEnabled = true;
2762 }
2763 if (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002764 this->disableStencil();
ethannicholas22793252016-01-30 09:59:10 -08002765 }
2766
egdaniel0e1853c2016-03-17 11:35:45 -07002767 for (int i = 0; i < meshCount; ++i) {
2768 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2769 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2770 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002771
egdaniel0e1853c2016-03-17 11:35:45 -07002772 const GrMesh& mesh = meshes[i];
2773 GrMesh::Iterator iter;
bsalomonfc9527a2016-08-29 09:18:39 -07002774 const GrNonInstancedMesh* nonInstMesh = iter.init(mesh);
egdaniel0e1853c2016-03-17 11:35:45 -07002775 do {
2776 size_t indexOffsetInBytes = 0;
bsalomonfc9527a2016-08-29 09:18:39 -07002777 this->setupGeometry(primProc, *nonInstMesh, &indexOffsetInBytes);
2778 if (nonInstMesh->isIndexed()) {
egdaniel0e1853c2016-03-17 11:35:45 -07002779 GrGLvoid* indices =
bsalomonfc9527a2016-08-29 09:18:39 -07002780 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes +
2781 sizeof(uint16_t) * nonInstMesh->startIndex());
egdaniel0e1853c2016-03-17 11:35:45 -07002782 // info.startVertex() was accounted for by setupGeometry.
bsalomonfc9527a2016-08-29 09:18:39 -07002783 if (this->glCaps().drawRangeElementsSupport()) {
2784 // We assume here that the batch that generated the mesh used the full
2785 // 0..vertexCount()-1 range.
2786 int start = 0;
2787 int end = nonInstMesh->vertexCount() - 1;
2788 GL_CALL(DrawRangeElements(gPrimitiveType2GLMode[nonInstMesh->primitiveType()],
2789 start, end,
2790 nonInstMesh->indexCount(),
2791 GR_GL_UNSIGNED_SHORT,
2792 indices));
2793 } else {
2794 GL_CALL(DrawElements(gPrimitiveType2GLMode[nonInstMesh->primitiveType()],
2795 nonInstMesh->indexCount(),
2796 GR_GL_UNSIGNED_SHORT,
2797 indices));
2798 }
egdaniel0e1853c2016-03-17 11:35:45 -07002799 } else {
2800 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account
2801 // for startVertex in the DrawElements case. So we always rely on setupGeometry to
2802 // have accounted for startVertex.
bsalomonfc9527a2016-08-29 09:18:39 -07002803 GL_CALL(DrawArrays(gPrimitiveType2GLMode[nonInstMesh->primitiveType()], 0,
2804 nonInstMesh->vertexCount()));
egdaniel0e1853c2016-03-17 11:35:45 -07002805 }
2806 fStats.incNumDraws();
bsalomonfc9527a2016-08-29 09:18:39 -07002807 } while ((nonInstMesh = iter.next()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002808 }
ethannicholas22793252016-01-30 09:59:10 -08002809
2810 if (fHWPLSEnabled && plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2811 // PLS draws always involve multiple draws, finishing up with a non-PLS
2812 // draw that writes to the color buffer. That draw ends up here; we wait
2813 // until after it is complete to actually disable PLS.
2814 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2815 fHWPLSEnabled = false;
2816 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002817 this->disableWindowRectangles();
ethannicholas22793252016-01-30 09:59:10 -08002818 }
2819
bsalomon@google.comd302f142011-03-03 13:54:13 +00002820#if SWAP_PER_DRAW
2821 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002822 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002823 aglSwapBuffers(aglGetCurrentContext());
2824 int set_a_break_pt_here = 9;
2825 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002826 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002827 SwapBuf();
2828 int set_a_break_pt_here = 9;
2829 SwapBuf();
2830 #endif
2831#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002832}
2833
cdaltone2e71c22016-04-07 18:13:29 -07002834void GrGLGpu::stampPLSSetupRect(const SkRect& bounds) {
caryclarkd6562002016-07-27 12:02:07 -07002835 SkASSERT(this->glCaps().glslCaps()->plsPathRenderingSupport());
ethannicholas22793252016-01-30 09:59:10 -08002836
cdaltone2e71c22016-04-07 18:13:29 -07002837 if (!fPLSSetupProgram.fProgram) {
2838 if (!this->createPLSSetupProgram()) {
2839 SkDebugf("Failed to create PLS setup program.\n");
2840 return;
2841 }
2842 }
2843
2844 GL_CALL(UseProgram(fPLSSetupProgram.fProgram));
2845 this->fHWVertexArrayState.setVertexArrayID(this, 0);
2846
2847 GrGLAttribArrayState* attribs = this->fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05002848 attribs->set(this, 0, fPLSSetupProgram.fArrayBuffer.get(), kVec2f_GrVertexAttribType,
cdaltone2e71c22016-04-07 18:13:29 -07002849 2 * sizeof(GrGLfloat), 0);
ethannicholas22793252016-01-30 09:59:10 -08002850 attribs->disableUnusedArrays(this, 0x1);
2851
cdaltone2e71c22016-04-07 18:13:29 -07002852 GL_CALL(Uniform4f(fPLSSetupProgram.fPosXformUniform, bounds.width(), bounds.height(),
2853 bounds.left(), bounds.top()));
ethannicholas22793252016-01-30 09:59:10 -08002854
2855 GrXferProcessor::BlendInfo blendInfo;
2856 blendInfo.reset();
2857 this->flushBlend(blendInfo, GrSwizzle());
2858 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07002859 this->flushDrawFace(GrDrawFace::kBoth);
ethannicholas22793252016-01-30 09:59:10 -08002860 if (!fHWStencilSettings.isDisabled()) {
2861 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2862 }
2863 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
2864 GL_CALL(UseProgram(fHWProgramID));
2865 if (!fHWStencilSettings.isDisabled()) {
2866 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2867 }
2868}
2869
egdaniel0e1853c2016-03-17 11:35:45 -07002870void GrGLGpu::setupPixelLocalStorage(const GrPipeline& pipeline,
2871 const GrPrimitiveProcessor& primProc) {
ethannicholas22793252016-01-30 09:59:10 -08002872 fPLSHasBeenUsed = true;
halcanary9d524f22016-03-29 09:03:52 -07002873 const SkRect& bounds =
egdaniel0e1853c2016-03-17 11:35:45 -07002874 static_cast<const GrPLSGeometryProcessor&>(primProc).getBounds();
ethannicholas22793252016-01-30 09:59:10 -08002875 // setup pixel local storage -- this means capturing and storing the current framebuffer color
2876 // and initializing the winding counts to zero
egdaniel0e1853c2016-03-17 11:35:45 -07002877 GrRenderTarget* rt = pipeline.getRenderTarget();
ethannicholas22793252016-01-30 09:59:10 -08002878 SkScalar width = SkIntToScalar(rt->width());
2879 SkScalar height = SkIntToScalar(rt->height());
2880 // dst rect edges in NDC (-1 to 1)
2881 // having some issues with rounding, just expand the bounds by 1 and trust the scissor to keep
2882 // it contained properly
2883 GrGLfloat dx0 = 2.0f * (bounds.left() - 1) / width - 1.0f;
2884 GrGLfloat dx1 = 2.0f * (bounds.right() + 1) / width - 1.0f;
2885 GrGLfloat dy0 = -2.0f * (bounds.top() - 1) / height + 1.0f;
2886 GrGLfloat dy1 = -2.0f * (bounds.bottom() + 1) / height + 1.0f;
2887 SkRect deviceBounds = SkRect::MakeXYWH(dx0, dy0, dx1 - dx0, dy1 - dy0);
halcanary9d524f22016-03-29 09:03:52 -07002888
ethannicholas22793252016-01-30 09:59:10 -08002889 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
cdaltone2e71c22016-04-07 18:13:29 -07002890 this->stampPLSSetupRect(deviceBounds);
ethannicholas22793252016-01-30 09:59:10 -08002891}
2892
bsalomon861e1032014-12-16 07:33:49 -08002893void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002894 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002895 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002896 // Some extensions automatically resolves the texture when it is read.
2897 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002898 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2899 fStats.incRenderTargetBinds();
2900 fStats.incRenderTargetBinds();
2901 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2902 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2903 // make sure we go through flushRenderTarget() since we've modified
2904 // the bound DRAW FBO ID.
2905 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002906 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002907 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002908
bsalomon@google.com347c3822013-05-01 20:10:01 +00002909 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002910 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002911 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002912 scissorState.set(dirtyRect);
2913 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002914 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002915 GL_CALL(ResolveMultisampleFramebuffer());
2916 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002917 int l, b, r, t;
2918 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2919 this->glCaps().blitFramebufferSupportFlags()) {
2920 l = 0;
2921 b = 0;
2922 r = target->width();
2923 t = target->height();
2924 } else {
2925 GrGLIRect rect;
2926 rect.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2927 dirtyRect.width(), dirtyRect.height(), target->origin());
2928 l = rect.fLeft;
2929 b = rect.fBottom;
2930 r = rect.fLeft + rect.fWidth;
2931 t = rect.fBottom + rect.fHeight;
2932 }
derekf8c8f71a2014-09-16 06:24:57 -07002933
2934 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002935 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002936 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002937 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002938 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002939 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002940 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002941 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002942 }
2943}
2944
bsalomon@google.com411dad02012-06-05 20:24:20 +00002945namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002946
bsalomon@google.com411dad02012-06-05 20:24:20 +00002947
2948GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002949 static const GrGLenum gTable[kGrStencilOpCount] = {
2950 GR_GL_KEEP, // kKeep
2951 GR_GL_ZERO, // kZero
2952 GR_GL_REPLACE, // kReplace
2953 GR_GL_INVERT, // kInvert
2954 GR_GL_INCR_WRAP, // kIncWrap
2955 GR_GL_DECR_WRAP, // kDecWrap
2956 GR_GL_INCR, // kIncClamp
2957 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002958 };
cdalton93a379b2016-05-11 13:58:08 -07002959 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2960 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2961 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2962 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2963 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2964 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2965 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2966 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2967 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2968 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002969}
2970
2971void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002972 const GrStencilSettings::Face& face,
2973 GrGLenum glFace) {
2974 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2975 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2976 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002977
cdalton93a379b2016-05-11 13:58:08 -07002978 GrGLint ref = face.fRef;
2979 GrGLint mask = face.fTestMask;
2980 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002981
2982 if (GR_GL_FRONT_AND_BACK == glFace) {
2983 // we call the combined func just in case separate stencil is not
2984 // supported.
2985 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2986 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002987 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002988 } else {
2989 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2990 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002991 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002992 }
2993}
2994}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002995
bsalomon3e791242014-12-17 13:43:13 -08002996void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002997 if (stencilSettings.isDisabled()) {
2998 this->disableStencil();
2999 } else if (fHWStencilSettings != stencilSettings) {
3000 if (kYes_TriState != fHWStencilTestEnabled) {
3001 GL_CALL(Enable(GR_GL_STENCIL_TEST));
3002 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00003003 }
csmartdaltonc7d85332016-10-26 10:13:46 -07003004 if (stencilSettings.isTwoSided()) {
3005 SkASSERT(this->caps()->twoSidedStencilSupport());
3006 set_gl_stencil(this->glInterface(),
3007 stencilSettings.front(),
3008 GR_GL_FRONT);
3009 set_gl_stencil(this->glInterface(),
3010 stencilSettings.back(),
3011 GR_GL_BACK);
3012 } else {
3013 set_gl_stencil(this->glInterface(),
3014 stencilSettings.front(),
3015 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00003016 }
joshualitta58fe352014-10-27 08:39:00 -07003017 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00003018 }
3019}
3020
csmartdaltonc7d85332016-10-26 10:13:46 -07003021void GrGLGpu::disableStencil() {
3022 if (kNo_TriState != fHWStencilTestEnabled) {
3023 GL_CALL(Disable(GR_GL_STENCIL_TEST));
3024 fHWStencilTestEnabled = kNo_TriState;
3025 fHWStencilSettings.invalidate();
3026 }
3027}
3028
cdaltonaf8bc7d2016-02-05 09:35:20 -08003029void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08003030 // rt is only optional if useHWAA is false.
3031 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07003032 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00003033
csmartdalton2b5f2cb2016-06-10 14:06:32 -07003034 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07003035 if (useHWAA) {
3036 if (kYes_TriState != fMSAAEnabled) {
3037 GL_CALL(Enable(GR_GL_MULTISAMPLE));
3038 fMSAAEnabled = kYes_TriState;
3039 }
3040 } else {
3041 if (kNo_TriState != fMSAAEnabled) {
3042 GL_CALL(Disable(GR_GL_MULTISAMPLE));
3043 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003044 }
3045 }
3046 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08003047
3048 if (0 != this->caps()->maxRasterSamples()) {
csmartdaltonf9635992016-08-10 11:09:07 -07003049 if (useHWAA && rt->isMixedSampled() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08003050 // Since stencil is disabled and we want more samples than are in the color buffer, we
3051 // need to tell the rasterizer explicitly how many to run.
3052 if (kYes_TriState != fHWRasterMultisampleEnabled) {
3053 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
3054 fHWRasterMultisampleEnabled = kYes_TriState;
3055 }
3056 if (rt->numStencilSamples() != fHWNumRasterSamples) {
3057 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
3058 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
3059 fHWNumRasterSamples = rt->numStencilSamples();
3060 }
3061 } else {
3062 if (kNo_TriState != fHWRasterMultisampleEnabled) {
3063 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
3064 fHWRasterMultisampleEnabled = kNo_TriState;
3065 }
3066 }
3067 } else {
csmartdaltonf9635992016-08-10 11:09:07 -07003068 SkASSERT(!useHWAA || !rt->isMixedSampled() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003069 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003070}
3071
bsalomon7f9b2e42016-01-12 13:29:26 -08003072void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07003073 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07003074 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07003075
cdalton8917d622015-05-06 13:40:21 -07003076 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08003077 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
3078 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07003079 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
3080 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07003081 if (blendOff) {
3082 if (kNo_TriState != fHWBlendState.fEnabled) {
3083 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07003084
3085 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
3086 // https://code.google.com/p/skia/issues/detail?id=3943
3087 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
3088 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
3089 SkASSERT(this->caps()->advancedBlendEquationSupport());
3090 // Set to any basic blending equation.
3091 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
3092 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
3093 fHWBlendState.fEquation = blend_equation;
3094 }
3095
egdanielb414f252014-07-29 13:15:47 -07003096 fHWBlendState.fEnabled = kNo_TriState;
3097 }
cdalton8917d622015-05-06 13:40:21 -07003098 return;
3099 }
3100
3101 if (kYes_TriState != fHWBlendState.fEnabled) {
3102 GL_CALL(Enable(GR_GL_BLEND));
3103 fHWBlendState.fEnabled = kYes_TriState;
3104 }
3105
3106 if (fHWBlendState.fEquation != equation) {
3107 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
3108 fHWBlendState.fEquation = equation;
3109 }
3110
3111 if (GrBlendEquationIsAdvanced(equation)) {
3112 SkASSERT(this->caps()->advancedBlendEquationSupport());
3113 // Advanced equations have no other blend state.
3114 return;
3115 }
3116
bsalomone63ffef2016-02-05 07:17:34 -08003117 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07003118 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
3119 gXfermodeCoeff2Blend[dstCoeff]));
3120 fHWBlendState.fSrcCoeff = srcCoeff;
3121 fHWBlendState.fDstCoeff = dstCoeff;
3122 }
3123
bsalomon7f9b2e42016-01-12 13:29:26 -08003124 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
3125 GrColor blendConst = blendInfo.fBlendConstant;
3126 blendConst = swizzle.applyTo(blendConst);
3127 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
3128 GrGLfloat c[4];
3129 GrColorToRGBAFloat(blendConst, c);
3130 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
3131 fHWBlendState.fConstColor = blendConst;
3132 fHWBlendState.fConstColorValid = true;
3133 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00003134 }
3135}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00003136
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003137static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00003138 static const GrGLenum gWrapModes[] = {
3139 GR_GL_CLAMP_TO_EDGE,
3140 GR_GL_REPEAT,
3141 GR_GL_MIRRORED_REPEAT
3142 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00003143 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00003144 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
3145 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
3146 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
3147 return gWrapModes[tm];
3148}
3149
egdanielb7e7d572015-11-04 04:23:53 -08003150static GrGLenum get_component_enum_from_char(char component) {
3151 switch (component) {
3152 case 'r':
3153 return GR_GL_RED;
3154 case 'g':
3155 return GR_GL_GREEN;
3156 case 'b':
3157 return GR_GL_BLUE;
3158 case 'a':
3159 return GR_GL_ALPHA;
3160 default:
3161 SkFAIL("Unsupported component");
3162 return 0;
3163 }
3164}
3165
3166/** If texture swizzling is available using tex parameters then it is preferred over mangling
3167 the generated shader code. This potentially allows greater reuse of cached shaders. */
3168static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08003169 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08003170 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08003171 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08003172 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08003173 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08003174 }
3175}
3176
brianosmana167e742016-05-24 06:18:48 -07003177void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -07003178 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07003179 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003180
reed856e9d92015-09-30 12:21:45 -07003181#ifdef SK_DEBUG
3182 if (!this->caps()->npotTextureTileSupport()) {
3183 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
3184 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
3185 if (tileX || tileY) {
3186 const int w = texture->width();
3187 const int h = texture->height();
3188 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3189 }
3190 }
3191#endif
3192
bsalomon@google.comb8670992012-07-25 21:27:09 +00003193 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3194 // 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 +00003195 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003196 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003197 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003198 this->onResolveRenderTarget(texRT);
3199 }
3200
robertphillips8abb3702016-08-31 14:04:06 -07003201 uint32_t textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003202 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003203 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003204 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003205 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003206 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003207 }
3208
bsalomon@google.com4c883782012-06-04 19:05:11 +00003209 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003210 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003211 bool setAll = timestamp < this->getResetTimestamp();
3212 GrGLTexture::TexParams newTexParams;
3213
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003214 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003215 GR_GL_NEAREST,
3216 GR_GL_LINEAR,
3217 GR_GL_LINEAR_MIPMAP_LINEAR
3218 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003219 static GrGLenum glMagFilterModes[] = {
3220 GR_GL_NEAREST,
3221 GR_GL_LINEAR,
3222 GR_GL_LINEAR
3223 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003224 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003225
3226 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3227 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3228 filterMode = GrTextureParams::kBilerp_FilterMode;
3229 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003230 }
bsalomonefd7d452014-10-23 14:17:46 -07003231
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003232 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3233 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003234
brianosman33f6b3f2016-06-02 05:49:21 -07003235 if (GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003236 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003237 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003238 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003239 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003240 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003241 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003242
brianosman33f6b3f2016-06-02 05:49:21 -07003243#ifdef SK_DEBUG
3244 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
3245 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3246 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3247 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003248 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3249 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3250 : SkDestinationSurfaceColorMode::kLegacy;
3251 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003252 }
3253 }
3254#endif
3255
cblume55f2d2d2016-02-26 13:20:48 -08003256 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3257
bsalomon@google.comb8670992012-07-25 21:27:09 +00003258 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3259 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003260 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003261 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003262 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003263 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003264 }
3265 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3266 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003267 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003268 }
cblume55f2d2d2016-02-26 13:20:48 -08003269 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003270 // These are not supported in ES2 contexts
3271 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3272 if (newTexParams.fMaxMipMapLevel != 0) {
3273 this->setTextureUnit(unitIdx);
3274 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3275 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3276 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3277 newTexParams.fMaxMipMapLevel));
3278 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3279 newTexParams.fMaxMipMapLevel));
3280 }
cblume55f2d2d2016-02-26 13:20:48 -08003281 }
3282 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003283 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003284 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003285 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003286 }
3287 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003288 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003289 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003290 }
bsalomoncdee0092016-01-08 13:20:12 -08003291 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003292 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3293 oldTexParams.fSwizzleRGBA,
3294 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003295 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003296 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003297 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003298}
3299
csmartdalton1897cfd2016-06-03 08:50:54 -07003300void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003301 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3302 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003303
3304 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3305
3306 if (!buffTex.fKnownBound) {
3307 if (!buffTex.fTextureID) {
3308 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3309 if (!buffTex.fTextureID) {
3310 return;
3311 }
3312 }
3313
3314 this->setTextureUnit(unitIdx);
3315 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3316
3317 buffTex.fKnownBound = true;
3318 }
3319
robertphillips8abb3702016-08-31 14:04:06 -07003320 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003321 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003322
3323 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003324 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3325 this->glCaps().configSizedInternalFormat(texelConfig),
3326 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003327
cdalton74b8d322016-04-11 14:47:28 -07003328 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003329 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003330
3331 if (this->glCaps().textureSwizzleSupport() &&
3332 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3333 GrGLenum glSwizzle[4];
3334 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3335 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3336 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3337 }
3338
3339 buffer->setHasAttachedToTexture();
3340 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3341 }
3342}
3343
brianosman33f6b3f2016-06-02 05:49:21 -07003344void GrGLGpu::generateMipmaps(const GrTextureParams& params, bool allowSRGBInputs,
3345 GrGLTexture* texture) {
3346 SkASSERT(texture);
3347
3348 // First, figure out if we need mips for this texture at all:
3349 GrTextureParams::FilterMode filterMode = params.filterMode();
3350
3351 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3352 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3353 filterMode = GrTextureParams::kBilerp_FilterMode;
3354 }
3355 }
3356
3357 if (GrTextureParams::kMipMap_FilterMode != filterMode) {
3358 return;
3359 }
3360
3361 // If this is an sRGB texture and the mips were previously built the "other" way
3362 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3363 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003364 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3365 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3366 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003367 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003368 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003369 texture->texturePriv().dirtyMipMaps(true);
3370 }
3371
3372 // If the mips aren't dirty, we're done:
3373 if (!texture->texturePriv().mipMapsAreDirty()) {
3374 return;
3375 }
3376
3377 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3378 // from the rt it will still be the last bound texture, but it needs resolving.
3379 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3380 if (texRT) {
3381 this->onResolveRenderTarget(texRT);
3382 }
3383
3384 GrGLenum target = texture->target();
3385 this->setScratchTextureUnit();
3386 GL_CALL(BindTexture(target, texture->textureID()));
3387
3388 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3389 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
3390 if (GrPixelConfigIsSRGB(texture->config())) {
3391 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT,
3392 allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT));
3393 }
3394
3395 // Either do manual mipmap generation or (if that fails), just rely on the driver:
3396 if (!this->generateMipmap(texture, allowSRGBInputs)) {
3397 GL_CALL(GenerateMipmap(target));
3398 }
3399
brianosmanfe199872016-06-13 07:59:48 -07003400 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003401 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3402 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003403 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003404
3405 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3406 texture->textureParamsModified();
3407}
3408
cdalton74b8d322016-04-11 14:47:28 -07003409void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3410 this->setTextureUnit(unitIdx);
3411 if (this->glStandard() == kGLES_GrGLStandard) {
3412 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3413 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3414 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3415 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3416 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3417 } else {
3418 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3419 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3420 reinterpret_cast<const GrGLint*>(swizzle)));
3421 }
3422}
3423
egdaniel080e6732014-12-22 07:35:52 -08003424void GrGLGpu::flushColorWrite(bool writeColor) {
3425 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003426 if (kNo_TriState != fHWWriteToColor) {
3427 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3428 GR_GL_FALSE, GR_GL_FALSE));
3429 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003430 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003431 } else {
3432 if (kYes_TriState != fHWWriteToColor) {
3433 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3434 fHWWriteToColor = kYes_TriState;
3435 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003436 }
bsalomon3e791242014-12-17 13:43:13 -08003437}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003438
robertphillips5fa7f302016-07-21 09:21:04 -07003439void GrGLGpu::flushDrawFace(GrDrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003440 if (fHWDrawFace != face) {
3441 switch (face) {
robertphillips5fa7f302016-07-21 09:21:04 -07003442 case GrDrawFace::kCCW:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003443 GL_CALL(Enable(GR_GL_CULL_FACE));
3444 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003445 break;
robertphillips5fa7f302016-07-21 09:21:04 -07003446 case GrDrawFace::kCW:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003447 GL_CALL(Enable(GR_GL_CULL_FACE));
3448 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003449 break;
robertphillips5fa7f302016-07-21 09:21:04 -07003450 case GrDrawFace::kBoth:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003451 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003452 break;
3453 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003454 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003455 }
bsalomon3e791242014-12-17 13:43:13 -08003456 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003457 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003458}
3459
bsalomon861e1032014-12-16 07:33:49 -08003460void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003461 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003462 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003463 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003464 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003465 }
3466}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003467
bsalomon861e1032014-12-16 07:33:49 -08003468void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003469 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003470 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003471 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3472 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3473 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003474 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003475 // clear out the this field so that if a program does use this unit it will rebind the correct
3476 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07003477 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003478}
3479
Brian Salomone5e7eb12016-10-14 16:18:33 -04003480// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
3481static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3482 const GrSurface* src,
3483 const SkIRect& srcRect,
3484 const SkIPoint& dstPoint,
3485 const GrGLGpu* gpu) {
3486 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003487 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3488 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003489 return false;
3490 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003491 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3492 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3493 const GrRenderTarget* dstRT = dst->asRenderTarget();
3494 const GrRenderTarget* srcRT = src->asRenderTarget();
3495 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3496 return false;
3497 }
3498 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3499 return false;
3500 }
3501 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3502 return false;
3503 }
3504 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3505 // We would mirror to compensate for origin changes. Note that copySurface is
3506 // specified such that the src and dst rects are the same.
3507 if (dst->origin() != src->origin()) {
3508 return false;
3509 }
3510 }
3511 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
3512 if (srcRT && srcRT->numColorSamples() && dstRT && !dstRT->numColorSamples()) {
3513 return false;
3514 }
3515 }
3516 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3517 if (dstRT && dstRT->numColorSamples() > 0) {
3518 return false;
3519 }
3520 }
3521 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3522 if (dst->config() != src->config()) {
3523 return false;
3524 }
3525 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3526 const GrRenderTarget* srcRT = src->asRenderTarget();
3527 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3528 return false;
3529 }
3530 }
3531 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3532 if (srcRT && srcRT->numColorSamples() &&
3533 (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop)) {
3534 return false;
3535 }
3536 }
3537 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003538}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003539
bsalomon7ea33f52015-11-22 14:51:00 -08003540static inline bool can_copy_texsubimage(const GrSurface* dst,
3541 const GrSurface* src,
3542 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003543 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3544 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3545 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003546 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003547 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3548 return false;
3549 }
3550 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3551 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3552 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003553 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003554 return false;
3555 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003556 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3557 // If the src is multisampled (and uses an extension where there is a separate MSAA
3558 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003559 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003560 return false;
3561 }
bsalomon7ea33f52015-11-22 14:51:00 -08003562
3563 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3564 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3565 // texture.
3566 if (!dstTex) {
3567 return false;
3568 }
3569
3570 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003571
bsalomon7ea33f52015-11-22 14:51:00 -08003572 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3573 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003574 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003575 !GrPixelConfigIsCompressed(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003576 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
bsalomon7ea33f52015-11-22 14:51:00 -08003577 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003578 return true;
3579 } else {
3580 return false;
3581 }
3582}
3583
3584// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3585// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003586void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3587 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003588 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003589 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003590 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003591 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003592 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003593 GrGLuint* tempFBOID;
3594 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003595
egdanield803f272015-03-18 13:01:52 -07003596 if (0 == *tempFBOID) {
3597 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003598 }
3599
egdanield803f272015-03-18 13:01:52 -07003600 fStats.incRenderTargetBinds();
3601 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3602 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003603 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003604 target,
robertphillips754f4e92014-09-18 13:52:08 -07003605 texID,
3606 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003607 viewport->fLeft = 0;
3608 viewport->fBottom = 0;
3609 viewport->fWidth = surface->width();
3610 viewport->fHeight = surface->height();
3611 } else {
egdanield803f272015-03-18 13:01:52 -07003612 fStats.incRenderTargetBinds();
3613 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003614 *viewport = rt->getViewport();
3615 }
egdaniel0f5f9672015-02-03 11:10:51 -08003616}
3617
Brian Salomon71d9d842016-11-03 13:42:00 -04003618void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3619 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003620 if (!surface->asRenderTarget()) {
3621 SkASSERT(surface->asTexture());
3622 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3623 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3624 GR_GL_COLOR_ATTACHMENT0,
3625 textureTarget,
3626 0,
3627 0));
3628 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003629}
3630
egdaniel4bcd62e2016-08-31 07:37:31 -07003631bool GrGLGpu::initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07003632 // If the src is a texture, we can implement the blit as a draw assuming the config is
3633 // renderable.
3634 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
3635 desc->fOrigin = kDefault_GrSurfaceOrigin;
3636 desc->fFlags = kRenderTarget_GrSurfaceFlag;
3637 desc->fConfig = src->config();
3638 return true;
3639 }
3640
bsalomon7ea33f52015-11-22 14:51:00 -08003641 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
3642 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
3643 // Not supported for FBO blit or CopyTexSubImage
3644 return false;
3645 }
3646
bsalomon6df86402015-06-01 10:41:49 -07003647 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08003648 // possible and we return false to fallback to creating a render target dst for render-to-
3649 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
3650 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
3651
bsalomon5c7d62d2016-06-08 10:02:42 -07003652 GrSurfaceOrigin originForBlitFramebuffer = kDefault_GrSurfaceOrigin;
Brian Salomone5e7eb12016-10-14 16:18:33 -04003653 if (this->glCaps().blitFramebufferSupportFlags() &
3654 GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag) {
bsalomon5c7d62d2016-06-08 10:02:42 -07003655 originForBlitFramebuffer = src->origin();
3656 }
3657
bsalomon@google.comeb851172013-04-15 13:51:00 +00003658 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003659 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003660 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003661 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
3662 // then we set up for that, otherwise fail.
Brian Salomon71d9d842016-11-03 13:42:00 -04003663 if (this->glCaps().canConfigBeFBOColorAttachment(kBGRA_8888_GrPixelConfig)) {
bsalomon5c7d62d2016-06-08 10:02:42 -07003664 desc->fOrigin = originForBlitFramebuffer;
bsalomonf90a02b2014-11-26 12:28:00 -08003665 desc->fConfig = kBGRA_8888_GrPixelConfig;
3666 return true;
3667 }
3668 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00003669 }
3670
egdaniel4bcd62e2016-08-31 07:37:31 -07003671 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src);
3672 if (srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003673 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
3674 // fail.
Brian Salomon71d9d842016-11-03 13:42:00 -04003675 if (this->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon5c7d62d2016-06-08 10:02:42 -07003676 desc->fOrigin = originForBlitFramebuffer;
bsalomonf90a02b2014-11-26 12:28:00 -08003677 desc->fConfig = src->config();
3678 return true;
3679 }
3680 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00003681 }
bsalomonf90a02b2014-11-26 12:28:00 -08003682
3683 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
3684 desc->fConfig = src->config();
3685 desc->fOrigin = src->origin();
3686 desc->fFlags = kNone_GrSurfaceFlags;
3687 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003688}
3689
joshualitt1cbdcde2015-08-21 11:53:29 -07003690bool GrGLGpu::onCopySurface(GrSurface* dst,
3691 GrSurface* src,
3692 const SkIRect& srcRect,
3693 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003694 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3695 // swizzle.
3696 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
3697 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
3698 return false;
3699 }
bsalomon083617b2016-02-12 12:10:14 -08003700 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3701 bool preferCopy = SkToBool(dst->asRenderTarget());
3702 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003703 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3704 return true;
3705 }
bsalomon5df6fee2015-05-18 06:26:15 -07003706 }
cblume61214052016-01-26 09:10:48 -08003707
bsalomon6df86402015-06-01 10:41:49 -07003708 if (can_copy_texsubimage(dst, src, this)) {
3709 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3710 return true;
3711 }
3712
Brian Salomone5e7eb12016-10-14 16:18:33 -04003713 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003714 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3715 }
3716
bsalomon083617b2016-02-12 12:10:14 -08003717 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003718 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3719 return true;
3720 }
bsalomon083617b2016-02-12 12:10:14 -08003721 }
3722
bsalomon6df86402015-06-01 10:41:49 -07003723 return false;
3724}
3725
cdaltone2e71c22016-04-07 18:13:29 -07003726bool GrGLGpu::createCopyProgram(int progIdx) {
cdaltonc08f1962016-02-12 12:14:06 -08003727 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
egdaniel990dbc82016-07-13 14:09:30 -07003728 static const GrSLType kSamplerTypes[3] = { kTexture2DSampler_GrSLType,
3729 kTextureExternalSampler_GrSLType,
3730 kTexture2DRectSampler_GrSLType };
3731 if (kTextureExternalSampler_GrSLType == kSamplerTypes[progIdx] &&
cdaltone2e71c22016-04-07 18:13:29 -07003732 !this->glCaps().glslCaps()->externalTextureSupport()) {
3733 return false;
cdaltondeacc972016-04-06 14:26:33 -07003734 }
egdaniel990dbc82016-07-13 14:09:30 -07003735 if (kTexture2DRectSampler_GrSLType == kSamplerTypes[progIdx] &&
cdaltone2e71c22016-04-07 18:13:29 -07003736 !this->glCaps().rectangleTextureSupport()) {
3737 return false;
3738 }
3739
3740 if (!fCopyProgramArrayBuffer) {
3741 static const GrGLfloat vdata[] = {
3742 0, 0,
3743 0, 1,
3744 1, 0,
3745 1, 1
3746 };
3747 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3748 kStatic_GrAccessPattern, vdata));
3749 }
3750 if (!fCopyProgramArrayBuffer) {
3751 return false;
3752 }
3753
3754 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3755 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3756 if (!fCopyPrograms[progIdx].fProgram) {
3757 return false;
3758 }
3759
3760 const char* version = glslCaps->versionDeclString();
3761 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3762 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3763 GrShaderVar::kUniform_TypeModifier);
3764 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
3765 GrShaderVar::kUniform_TypeModifier);
3766 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[progIdx],
3767 GrShaderVar::kUniform_TypeModifier);
3768 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
3769 GrShaderVar::kVaryingOut_TypeModifier);
3770 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3771 GrShaderVar::kOut_TypeModifier);
3772
3773 SkString vshaderTxt(version);
3774 if (glslCaps->noperspectiveInterpolationSupport()) {
3775 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3776 vshaderTxt.appendf("#extension %s : require\n", extension);
3777 }
3778 vTexCoord.addModifier("noperspective");
3779 }
3780
3781 aVertex.appendDecl(glslCaps, &vshaderTxt);
3782 vshaderTxt.append(";");
3783 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
3784 vshaderTxt.append(";");
3785 uPosXform.appendDecl(glslCaps, &vshaderTxt);
3786 vshaderTxt.append(";");
3787 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
3788 vshaderTxt.append(";");
3789
3790 vshaderTxt.append(
3791 "// Copy Program VS\n"
3792 "void main() {"
3793 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3794 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3795 " gl_Position.zw = vec2(0, 1);"
3796 "}"
3797 );
3798
3799 SkString fshaderTxt(version);
3800 if (glslCaps->noperspectiveInterpolationSupport()) {
3801 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3802 fshaderTxt.appendf("#extension %s : require\n", extension);
3803 }
3804 }
egdaniel990dbc82016-07-13 14:09:30 -07003805 if (kSamplerTypes[progIdx] == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003806 fshaderTxt.appendf("#extension %s : require\n",
3807 glslCaps->externalTextureExtensionString());
3808 }
3809 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
3810 &fshaderTxt);
3811 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3812 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
3813 fshaderTxt.append(";");
3814 uTexture.appendDecl(glslCaps, &fshaderTxt);
3815 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003816 fshaderTxt.appendf(
3817 "// Copy Program FS\n"
3818 "void main() {"
ethannicholas5961bc92016-10-12 06:39:56 -07003819 " sk_FragColor = %s(u_texture, v_texCoord);"
cdaltone2e71c22016-04-07 18:13:29 -07003820 "}",
cdaltone2e71c22016-04-07 18:13:29 -07003821 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[progIdx], this->glslGeneration())
3822 );
3823
3824 const char* str;
3825 GrGLint length;
3826
3827 str = vshaderTxt.c_str();
3828 length = SkToInt(vshaderTxt.size());
3829 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3830 GR_GL_VERTEX_SHADER, &str, &length, 1,
3831 &fStats);
3832
3833 str = fshaderTxt.c_str();
3834 length = SkToInt(fshaderTxt.size());
3835 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3836 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3837 &fStats);
3838
3839 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3840
3841 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3842 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3843 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3844 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3845 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3846 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3847
3848 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3849
3850 GL_CALL(DeleteShader(vshader));
3851 GL_CALL(DeleteShader(fshader));
3852
3853 return true;
bsalomon6df86402015-06-01 10:41:49 -07003854}
3855
brianosman33f6b3f2016-06-02 05:49:21 -07003856bool GrGLGpu::createMipmapProgram(int progIdx) {
3857 const bool oddWidth = SkToBool(progIdx & 0x2);
3858 const bool oddHeight = SkToBool(progIdx & 0x1);
3859 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3860
3861 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
3862
3863 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3864 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3865 if (!fMipmapPrograms[progIdx].fProgram) {
3866 return false;
3867 }
3868
3869 const char* version = glslCaps->versionDeclString();
3870 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3871 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3872 GrShaderVar::kUniform_TypeModifier);
egdaniel990dbc82016-07-13 14:09:30 -07003873 GrGLSLShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3874 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003875 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
3876 GrGLSLShaderVar vTexCoords[] = {
3877 GrGLSLShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3878 GrGLSLShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3879 GrGLSLShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3880 GrGLSLShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3881 };
3882 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3883 GrShaderVar::kOut_TypeModifier);
3884
3885 SkString vshaderTxt(version);
3886 if (glslCaps->noperspectiveInterpolationSupport()) {
3887 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3888 vshaderTxt.appendf("#extension %s : require\n", extension);
3889 }
3890 vTexCoords[0].addModifier("noperspective");
3891 vTexCoords[1].addModifier("noperspective");
3892 vTexCoords[2].addModifier("noperspective");
3893 vTexCoords[3].addModifier("noperspective");
3894 }
3895
3896 aVertex.appendDecl(glslCaps, &vshaderTxt);
3897 vshaderTxt.append(";");
3898 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
3899 vshaderTxt.append(";");
3900 for (int i = 0; i < numTaps; ++i) {
3901 vTexCoords[i].appendDecl(glslCaps, &vshaderTxt);
3902 vshaderTxt.append(";");
3903 }
3904
3905 vshaderTxt.append(
3906 "// Mipmap Program VS\n"
3907 "void main() {"
3908 " gl_Position.xy = a_vertex * vec2(2, 2) - vec2(1, 1);"
3909 " gl_Position.zw = vec2(0, 1);"
3910 );
3911
3912 // Insert texture coordinate computation:
3913 if (oddWidth && oddHeight) {
3914 vshaderTxt.append(
3915 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
3916 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + vec2(u_texCoordXform.x, 0);"
3917 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + vec2(0, u_texCoordXform.z);"
3918 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3919 );
3920 } else if (oddWidth) {
3921 vshaderTxt.append(
3922 " v_texCoord0 = a_vertex.xy * vec2(u_texCoordXform.y, 1);"
3923 " v_texCoord1 = a_vertex.xy * vec2(u_texCoordXform.y, 1) + vec2(u_texCoordXform.x, 0);"
3924 );
3925 } else if (oddHeight) {
3926 vshaderTxt.append(
3927 " v_texCoord0 = a_vertex.xy * vec2(1, u_texCoordXform.w);"
3928 " v_texCoord1 = a_vertex.xy * vec2(1, u_texCoordXform.w) + vec2(0, u_texCoordXform.z);"
3929 );
3930 } else {
3931 vshaderTxt.append(
3932 " v_texCoord0 = a_vertex.xy;"
3933 );
3934 }
3935
3936 vshaderTxt.append("}");
3937
3938 SkString fshaderTxt(version);
3939 if (glslCaps->noperspectiveInterpolationSupport()) {
3940 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3941 fshaderTxt.appendf("#extension %s : require\n", extension);
3942 }
3943 }
3944 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
3945 &fshaderTxt);
3946 for (int i = 0; i < numTaps; ++i) {
3947 vTexCoords[i].setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3948 vTexCoords[i].appendDecl(glslCaps, &fshaderTxt);
3949 fshaderTxt.append(";");
3950 }
3951 uTexture.appendDecl(glslCaps, &fshaderTxt);
3952 fshaderTxt.append(";");
egdaniel990dbc82016-07-13 14:09:30 -07003953 const char* sampleFunction = GrGLSLTexture2DFunctionName(kVec2f_GrSLType,
3954 kTexture2DSampler_GrSLType,
brianosman33f6b3f2016-06-02 05:49:21 -07003955 this->glslGeneration());
3956 fshaderTxt.append(
3957 "// Mipmap Program FS\n"
3958 "void main() {"
3959 );
3960
3961 if (oddWidth && oddHeight) {
3962 fshaderTxt.appendf(
ethannicholas5961bc92016-10-12 06:39:56 -07003963 " sk_FragColor = (%s(u_texture, v_texCoord0) + %s(u_texture, v_texCoord1) + "
3964 " %s(u_texture, v_texCoord2) + %s(u_texture, v_texCoord3)) * 0.25;",
3965 sampleFunction, sampleFunction, sampleFunction, sampleFunction
brianosman33f6b3f2016-06-02 05:49:21 -07003966 );
3967 } else if (oddWidth || oddHeight) {
3968 fshaderTxt.appendf(
ethannicholas5961bc92016-10-12 06:39:56 -07003969 " sk_FragColor = (%s(u_texture, v_texCoord0) + %s(u_texture, v_texCoord1)) * 0.5;",
3970 sampleFunction, sampleFunction
brianosman33f6b3f2016-06-02 05:49:21 -07003971 );
3972 } else {
3973 fshaderTxt.appendf(
ethannicholas5961bc92016-10-12 06:39:56 -07003974 " sk_FragColor = %s(u_texture, v_texCoord0);",
3975 sampleFunction
brianosman33f6b3f2016-06-02 05:49:21 -07003976 );
3977 }
3978
3979 fshaderTxt.append("}");
3980
3981 const char* str;
3982 GrGLint length;
3983
3984 str = vshaderTxt.c_str();
3985 length = SkToInt(vshaderTxt.size());
3986 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3987 GR_GL_VERTEX_SHADER, &str, &length, 1,
3988 &fStats);
3989
3990 str = fshaderTxt.c_str();
3991 length = SkToInt(fshaderTxt.size());
3992 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3993 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3994 &fStats);
3995
3996 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3997
3998 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3999 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
4000 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4001 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
4002
4003 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
4004
4005 GL_CALL(DeleteShader(vshader));
4006 GL_CALL(DeleteShader(fshader));
4007
4008 return true;
4009}
4010
cdaltone2e71c22016-04-07 18:13:29 -07004011bool GrGLGpu::createWireRectProgram() {
4012 if (!fWireRectArrayBuffer) {
4013 static const GrGLfloat vdata[] = {
4014 0, 0,
4015 0, 1,
4016 1, 1,
4017 1, 0
4018 };
4019 fWireRectArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
4020 kStatic_GrAccessPattern, vdata));
4021 if (!fWireRectArrayBuffer) {
4022 return false;
4023 }
4024 }
4025
bsalomon6dea83f2015-12-03 12:58:06 -08004026 SkASSERT(!fWireRectProgram.fProgram);
cdaltone2e71c22016-04-07 18:13:29 -07004027 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
4028 if (!fWireRectProgram.fProgram) {
4029 return false;
4030 }
4031
bsalomon6dea83f2015-12-03 12:58:06 -08004032 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
4033 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
4034 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
4035 const char* version = this->glCaps().glslCaps()->versionDeclString();
4036
4037 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
4038 // program is used with a vbo containing the unit square. Vertices are computed from the rect
4039 // uniform using the 4 vbo vertices.
4040 SkString vshaderTxt(version);
4041 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
4042 vshaderTxt.append(";");
4043 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
4044 vshaderTxt.append(";");
4045 vshaderTxt.append(
4046 "// Wire Rect Program VS\n"
4047 "void main() {"
4048 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
4049 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
4050 " gl_Position.zw = vec2(0, 1);"
4051 "}"
4052 );
4053
4054 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
4055
4056 SkString fshaderTxt(version);
4057 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
4058 *this->glCaps().glslCaps(),
4059 &fshaderTxt);
4060 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
4061 fshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08004062 fshaderTxt.appendf(
4063 "// Write Rect Program FS\n"
4064 "void main() {"
ethannicholas5961bc92016-10-12 06:39:56 -07004065 " sk_FragColor = %s;"
bsalomon6dea83f2015-12-03 12:58:06 -08004066 "}",
bsalomon6dea83f2015-12-03 12:58:06 -08004067 uColor.c_str()
4068 );
4069
bsalomon6dea83f2015-12-03 12:58:06 -08004070 const char* str;
4071 GrGLint length;
4072
4073 str = vshaderTxt.c_str();
4074 length = SkToInt(vshaderTxt.size());
4075 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
4076 GR_GL_VERTEX_SHADER, &str, &length, 1,
4077 &fStats);
4078
4079 str = fshaderTxt.c_str();
4080 length = SkToInt(fshaderTxt.size());
4081 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
4082 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
4083 &fStats);
4084
4085 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
4086
4087 GL_CALL_RET(fWireRectProgram.fColorUniform,
4088 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
4089 GL_CALL_RET(fWireRectProgram.fRectUniform,
4090 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
4091 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
4092
4093 GL_CALL(DeleteShader(vshader));
4094 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07004095
4096 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08004097}
4098
4099void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08004100 // TODO: This should swizzle the output to match dst's config, though it is a debugging
4101 // visualization.
4102
bsalomon6dea83f2015-12-03 12:58:06 -08004103 this->handleDirtyContext();
4104 if (!fWireRectProgram.fProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07004105 if (!this->createWireRectProgram()) {
4106 SkDebugf("Failed to create wire rect program.\n");
4107 return;
4108 }
bsalomon6dea83f2015-12-03 12:58:06 -08004109 }
4110
4111 int w = rt->width();
4112 int h = rt->height();
4113
4114 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
4115 // whether the render target is flipped or not.
4116 GrGLfloat edges[4];
4117 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
4118 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
4119 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
4120 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
4121 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
4122 } else {
4123 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
4124 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
4125 }
4126 edges[0] = 2 * edges[0] / w - 1.0f;
4127 edges[1] = 2 * edges[1] / h - 1.0f;
4128 edges[2] = 2 * edges[2] / w - 1.0f;
4129 edges[3] = 2 * edges[3] / h - 1.0f;
4130
4131 GrGLfloat channels[4];
4132 static const GrGLfloat scale255 = 1.f / 255.f;
4133 channels[0] = GrColorUnpackR(color) * scale255;
4134 channels[1] = GrColorUnpackG(color) * scale255;
4135 channels[2] = GrColorUnpackB(color) * scale255;
4136 channels[3] = GrColorUnpackA(color) * scale255;
4137
4138 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
4139 this->flushRenderTarget(glRT, &rect);
4140
4141 GL_CALL(UseProgram(fWireRectProgram.fProgram));
4142 fHWProgramID = fWireRectProgram.fProgram;
4143
cdaltone2e71c22016-04-07 18:13:29 -07004144 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08004145
cdaltone2e71c22016-04-07 18:13:29 -07004146 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05004147 attribs->set(this, 0, fWireRectArrayBuffer.get(), kVec2f_GrVertexAttribType,
4148 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08004149 attribs->disableUnusedArrays(this, 0x1);
4150
4151 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
4152 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
4153
4154 GrXferProcessor::BlendInfo blendInfo;
4155 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004156 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08004157 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004158 this->flushDrawFace(GrDrawFace::kBoth);
cdaltonaf8bc7d2016-02-05 09:35:20 -08004159 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08004160 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004161 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004162 this->disableStencil();
bsalomon6dea83f2015-12-03 12:58:06 -08004163
4164 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
4165}
4166
4167
cdaltone2e71c22016-04-07 18:13:29 -07004168bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07004169 GrSurface* src,
4170 const SkIRect& srcRect,
4171 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07004172 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
4173 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
4174
4175 if (!fCopyPrograms[progIdx].fProgram) {
4176 if (!this->createCopyProgram(progIdx)) {
4177 SkDebugf("Failed to create copy program.\n");
4178 return false;
4179 }
4180 }
4181
bsalomon6df86402015-06-01 10:41:49 -07004182 int w = srcRect.width();
4183 int h = srcRect.height();
4184
bsalomon6df86402015-06-01 10:41:49 -07004185 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07004186 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07004187
bsalomon083617b2016-02-12 12:10:14 -08004188 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004189 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004190 this->flushViewport(dstVP);
4191 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4192
bsalomon6df86402015-06-01 10:41:49 -07004193 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07004194
bsalomon7ea33f52015-11-22 14:51:00 -08004195 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
4196 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07004197
cdaltone2e71c22016-04-07 18:13:29 -07004198 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07004199
cdaltone2e71c22016-04-07 18:13:29 -07004200 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05004201 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
4202 2 * sizeof(GrGLfloat), 0);
bsalomond6246342015-06-04 13:57:00 -07004203 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07004204
4205 // dst rect edges in NDC (-1 to 1)
4206 int dw = dst->width();
4207 int dh = dst->height();
4208 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4209 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4210 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4211 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
4212 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4213 dy0 = -dy0;
4214 dy1 = -dy1;
4215 }
4216
bsalomone5286e02016-01-14 09:24:09 -08004217 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4218 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4219 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4220 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07004221 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07004222 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08004223 sy0 = sh - sy0;
4224 sy1 = sh - sy1;
4225 }
4226 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
4227 GrGLenum srcTarget = srcTex->target();
4228 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
4229 int sw = src->width();
4230 sx0 /= sw;
4231 sx1 /= sw;
4232 sy0 /= sh;
4233 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004234 }
4235
bsalomon7ea33f52015-11-22 14:51:00 -08004236 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4237 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4238 sx1 - sx0, sy1 - sy0, sx0, sy0));
4239 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004240
4241 GrXferProcessor::BlendInfo blendInfo;
4242 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004243 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004244 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004245 this->flushDrawFace(GrDrawFace::kBoth);
bsalomon083617b2016-02-12 12:10:14 -08004246 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004247 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004248 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004249 this->disableStencil();
bsalomon6df86402015-06-01 10:41:49 -07004250
4251 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004252 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08004253 this->didWriteToSurface(dst, &dstRect);
4254
cdaltone2e71c22016-04-07 18:13:29 -07004255 return true;
bsalomon6df86402015-06-01 10:41:49 -07004256}
4257
4258void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
4259 GrSurface* src,
4260 const SkIRect& srcRect,
4261 const SkIPoint& dstPoint) {
4262 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07004263 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004264 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004265 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004266 SkASSERT(dstTex);
4267 // We modified the bound FBO
4268 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4269 GrGLIRect srcGLRect;
4270 srcGLRect.setRelativeTo(srcVP,
4271 srcRect.fLeft,
4272 srcRect.fTop,
4273 srcRect.width(),
4274 srcRect.height(),
4275 src->origin());
4276
4277 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004278 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004279 GrGLint dstY;
4280 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4281 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4282 } else {
4283 dstY = dstPoint.fY;
4284 }
bsalomon10528f12015-10-14 12:54:52 -07004285 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004286 dstPoint.fX, dstY,
4287 srcGLRect.fLeft, srcGLRect.fBottom,
4288 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004289 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004290 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4291 srcRect.width(), srcRect.height());
4292 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004293}
4294
4295bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
4296 GrSurface* src,
4297 const SkIRect& srcRect,
4298 const SkIPoint& dstPoint) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04004299 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07004300 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4301 srcRect.width(), srcRect.height());
4302 if (dst == src) {
4303 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4304 return false;
mtklein404b3b22015-05-18 09:29:10 -07004305 }
bsalomon5df6fee2015-05-18 06:26:15 -07004306 }
bsalomon6df86402015-06-01 10:41:49 -07004307
bsalomon6df86402015-06-01 10:41:49 -07004308 GrGLIRect dstVP;
4309 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004310 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4311 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004312 // We modified the bound FBO
4313 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4314 GrGLIRect srcGLRect;
4315 GrGLIRect dstGLRect;
4316 srcGLRect.setRelativeTo(srcVP,
4317 srcRect.fLeft,
4318 srcRect.fTop,
4319 srcRect.width(),
4320 srcRect.height(),
4321 src->origin());
4322 dstGLRect.setRelativeTo(dstVP,
4323 dstRect.fLeft,
4324 dstRect.fTop,
4325 dstRect.width(),
4326 dstRect.height(),
4327 dst->origin());
4328
4329 // BlitFrameBuffer respects the scissor, so disable it.
4330 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004331 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004332
4333 GrGLint srcY0;
4334 GrGLint srcY1;
4335 // Does the blit need to y-mirror or not?
4336 if (src->origin() == dst->origin()) {
4337 srcY0 = srcGLRect.fBottom;
4338 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4339 } else {
4340 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4341 srcY1 = srcGLRect.fBottom;
4342 }
4343 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4344 srcY0,
4345 srcGLRect.fLeft + srcGLRect.fWidth,
4346 srcY1,
4347 dstGLRect.fLeft,
4348 dstGLRect.fBottom,
4349 dstGLRect.fLeft + dstGLRect.fWidth,
4350 dstGLRect.fBottom + dstGLRect.fHeight,
4351 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004352 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4353 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004354 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004355 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004356}
4357
brianosman33f6b3f2016-06-02 05:49:21 -07004358// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4359// Uses draw calls to do a series of downsample operations to successive mips.
4360// If this returns false, then the calling code falls back to using glGenerateMipmap.
4361bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
brianosman09563ce2016-06-02 08:59:34 -07004362 // Our iterative downsample requires the ability to limit which level we're sampling:
4363 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004364 return false;
4365 }
4366
4367 // Mipmaps are only supported on 2D textures:
4368 if (GR_GL_TEXTURE_2D != texture->target()) {
4369 return false;
4370 }
4371
4372 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004373 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004374 return false;
4375 }
4376
brianosman33f6b3f2016-06-02 05:49:21 -07004377 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4378 if (GrPixelConfigIsSRGB(texture->config())) {
4379 // If we have write-control, just set the state that we want:
4380 if (this->glCaps().srgbWriteControl()) {
4381 this->flushFramebufferSRGB(gammaCorrect);
4382 } else if (!gammaCorrect) {
4383 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4384 return false;
4385 }
4386 }
4387
4388 int width = texture->width();
4389 int height = texture->height();
4390 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4391
4392 // Define all mips, if we haven't previously done so:
4393 if (0 == texture->texturePriv().maxMipMapLevel()) {
4394 GrGLenum internalFormat;
4395 GrGLenum externalFormat;
4396 GrGLenum externalType;
4397 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4398 &internalFormat, &externalFormat, &externalType)) {
4399 return false;
4400 }
4401
4402 for (GrGLint level = 1; level < levelCount; ++level) {
4403 // Define the next mip:
4404 width = SkTMax(1, width / 2);
4405 height = SkTMax(1, height / 2);
4406 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4407 width, height, 0,
4408 externalFormat, externalType, nullptr));
4409 }
4410 }
4411
4412 // Create (if necessary), then bind temporary FBO:
4413 if (0 == fTempDstFBOID) {
4414 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4415 }
4416 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
4417 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4418
4419 // Bind the texture, to get things configured for filtering.
4420 // We'll be changing our base level further below:
4421 this->setTextureUnit(0);
4422 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
4423 this->bindTexture(0, params, gammaCorrect, texture);
4424
4425 // Vertex data:
4426 if (!fMipmapProgramArrayBuffer) {
4427 static const GrGLfloat vdata[] = {
4428 0, 0,
4429 0, 1,
4430 1, 0,
4431 1, 1
4432 };
4433 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4434 kVertex_GrBufferType,
4435 kStatic_GrAccessPattern, vdata));
4436 }
4437 if (!fMipmapProgramArrayBuffer) {
4438 return false;
4439 }
4440
4441 fHWVertexArrayState.setVertexArrayID(this, 0);
4442
4443 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Hal Canary144caf52016-11-07 17:57:18 -05004444 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004445 2 * sizeof(GrGLfloat), 0);
4446 attribs->disableUnusedArrays(this, 0x1);
4447
4448 // Set "simple" state once:
4449 GrXferProcessor::BlendInfo blendInfo;
4450 blendInfo.reset();
4451 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4452 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004453 this->flushDrawFace(GrDrawFace::kBoth);
brianosman33f6b3f2016-06-02 05:49:21 -07004454 this->flushHWAAState(nullptr, false, false);
4455 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004456 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004457 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004458
4459 // Do all the blits:
4460 width = texture->width();
4461 height = texture->height();
4462 GrGLIRect viewport;
4463 viewport.fLeft = 0;
4464 viewport.fBottom = 0;
4465 for (GrGLint level = 1; level < levelCount; ++level) {
4466 // Get and bind the program for this particular downsample (filter shape can vary):
4467 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4468 if (!fMipmapPrograms[progIdx].fProgram) {
4469 if (!this->createMipmapProgram(progIdx)) {
4470 SkDebugf("Failed to create mipmap program.\n");
4471 return false;
4472 }
4473 }
4474 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4475 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4476
4477 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4478 const float invWidth = 1.0f / width;
4479 const float invHeight = 1.0f / height;
4480 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4481 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4482 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4483
4484 // Only sample from previous mip
4485 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4486
4487 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4488 GR_GL_TEXTURE_2D, texture->textureID(), level));
4489
4490 width = SkTMax(1, width / 2);
4491 height = SkTMax(1, height / 2);
4492 viewport.fWidth = width;
4493 viewport.fHeight = height;
4494 this->flushViewport(viewport);
4495
4496 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4497 }
4498
4499 // Unbind:
4500 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4501 GR_GL_TEXTURE_2D, 0, 0));
4502
4503 return true;
4504}
4505
csmartdaltonc25c5d72016-11-01 07:03:59 -07004506void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
4507 int* effectiveSampleCnt, SamplePattern* samplePattern) {
csmartdaltonf9635992016-08-10 11:09:07 -07004508 SkASSERT(!rt->isMixedSampled() || rt->renderTargetPriv().getStencilAttachment() ||
cdalton28f45b92016-03-07 13:58:26 -08004509 stencil.isDisabled());
4510
4511 this->flushStencil(stencil);
4512 this->flushHWAAState(rt, true, !stencil.isDisabled());
4513 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4514
4515 if (0 != this->caps()->maxRasterSamples()) {
4516 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4517 } else {
4518 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4519 }
4520
4521 SkASSERT(*effectiveSampleCnt >= rt->desc().fSampleCnt);
4522
4523 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004524 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004525 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4526 GrGLfloat pos[2];
4527 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4528 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004529 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004530 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004531 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004532 }
4533 }
4534 }
4535}
4536
cdalton231c5fd2015-05-13 12:35:36 -07004537void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004538 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004539 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004540 case kTexture_GrXferBarrierType: {
4541 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4542 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4543 // The render target uses separate storage so no need for glTextureBarrier.
4544 // FIXME: The render target will resolve automatically when its texture is bound,
4545 // but we could resolve only the bounds that will be read if we do it here instead.
4546 return;
4547 }
cdalton9954bc32015-04-29 14:17:00 -07004548 SkASSERT(this->caps()->textureBarrierSupport());
4549 GL_CALL(TextureBarrier());
4550 return;
cdalton231c5fd2015-05-13 12:35:36 -07004551 }
cdalton8917d622015-05-06 13:40:21 -07004552 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004553 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004554 this->caps()->blendEquationSupport());
4555 GL_CALL(BlendBarrier());
4556 return;
bsalomoncb02b382015-08-12 11:14:50 -07004557 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004558 }
4559}
4560
jvanverth88957922015-07-14 11:02:52 -07004561GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004562 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004563 if (!this->caps()->isConfigTexturable(config)) {
4564 return false;
4565 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004566 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004567 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004568 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004569 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004570 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4571 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004572 GL_CALL(BindTexture(info->fTarget, info->fID));
bsalomone63ffef2016-02-05 07:17:34 -08004573 fHWBoundTextureUniqueIDs[0] = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004574 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4575 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4576 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4577 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004578
bsalomon76148af2016-01-12 11:13:47 -08004579 GrGLenum internalFormat;
4580 GrGLenum externalFormat;
4581 GrGLenum externalType;
4582
4583 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4584 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004585 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004586 }
jvanverth672bb7f2015-07-13 07:19:57 -07004587
bsalomon091f60c2015-11-10 11:54:56 -08004588 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004589 externalType, pixels));
4590
Ben Wagner18b61f92016-10-25 10:44:02 -04004591 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004592}
4593
jvanverth88957922015-07-14 11:02:52 -07004594bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004595 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004596
4597 GrGLboolean result;
4598 GL_CALL_RET(result, IsTexture(texID));
4599
4600 return (GR_GL_TRUE == result);
4601}
4602
bsalomone63ffef2016-02-05 07:17:34 -08004603void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004604 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004605 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004606
bsalomon67d76202015-11-11 12:40:42 -08004607 if (!abandonTexture) {
4608 GL_CALL(DeleteTextures(1, &texID));
4609 }
jvanverth672bb7f2015-07-13 07:19:57 -07004610}
4611
joshualitt8fd844f2015-12-02 13:36:47 -08004612void GrGLGpu::resetShaderCacheForTesting() const {
4613 fProgramCache->abandon();
4614}
4615
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004616///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004617
cdaltone2e71c22016-04-07 18:13:29 -07004618GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004619 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004620 GrGLAttribArrayState* attribState;
4621
cdaltone2e71c22016-04-07 18:13:29 -07004622 if (gpu->glCaps().isCoreProfile()) {
4623 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004624 GrGLuint arrayID;
4625 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4626 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004627 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004628 }
cdaltone2e71c22016-04-07 18:13:29 -07004629 if (ibuf) {
4630 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004631 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004632 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004633 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004634 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004635 if (ibuf) {
4636 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4637 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004638 } else {
4639 this->setVertexArrayID(gpu, 0);
4640 }
4641 int attrCount = gpu->glCaps().maxVertexAttributes();
4642 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4643 fDefaultVertexArrayAttribState.resize(attrCount);
4644 }
4645 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004646 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004647 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004648}
bsalomone179a912016-01-20 06:18:10 -08004649
4650bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
4651 GrTextureProducer::CopyParams* copyParams) const {
4652 if (textureParams.isTiled() ||
4653 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
4654 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
4655 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4656 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
4657 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
4658 copyParams->fWidth = texture->width();
4659 copyParams->fHeight = texture->height();
4660 return true;
4661 }
4662 }
4663 return false;
4664}
jvanverth84741b32016-09-30 08:39:02 -07004665
4666GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() const {
4667 GrGLsync fence;
4668 GL_CALL_RET(fence, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4669 return (GrFence)fence;
4670}
4671
4672bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) const {
4673 GrGLenum result;
4674 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4675 return (GR_GL_CONDITION_SATISFIED == result);
4676}
4677
4678void GrGLGpu::deleteFence(GrFence fence) const {
4679 GL_CALL(DeleteSync((GrGLsync)fence));
4680}