blob: e002d5101bab5a114a75f5ea8d0e219989d0a675 [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"
cblume55f2d2d2016-02-26 13:20:48 -080028#include "SkMipMap.h"
29#include "SkPixmap.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000030#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000031#include "SkTemplates.h"
cblume55f2d2d2016-02-26 13:20:48 -080032#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000033
bsalomon@google.com0b77d682011-08-19 13:28:54 +000034#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000035#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000036
reed@google.comac10a2d2010-12-22 21:39:39 +000037#define SKIP_CACHE_CHECK true
38
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000039#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
40 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
41 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
42 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000044 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
45 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
46 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
47#endif
48
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000049///////////////////////////////////////////////////////////////////////////////
50
csmartdaltona7f29642016-07-07 08:49:11 -070051using gr_instanced::InstancedRendering;
52using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070053
cdalton8917d622015-05-06 13:40:21 -070054static const GrGLenum gXfermodeEquation2Blend[] = {
55 // Basic OpenGL blend equations.
56 GR_GL_FUNC_ADD,
57 GR_GL_FUNC_SUBTRACT,
58 GR_GL_FUNC_REVERSE_SUBTRACT,
59
60 // GL_KHR_blend_equation_advanced.
61 GR_GL_SCREEN,
62 GR_GL_OVERLAY,
63 GR_GL_DARKEN,
64 GR_GL_LIGHTEN,
65 GR_GL_COLORDODGE,
66 GR_GL_COLORBURN,
67 GR_GL_HARDLIGHT,
68 GR_GL_SOFTLIGHT,
69 GR_GL_DIFFERENCE,
70 GR_GL_EXCLUSION,
71 GR_GL_MULTIPLY,
72 GR_GL_HSL_HUE,
73 GR_GL_HSL_SATURATION,
74 GR_GL_HSL_COLOR,
75 GR_GL_HSL_LUMINOSITY
76};
77GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
78GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
79GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
80GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
81GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
82GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
83GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
84GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
85GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
86GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
87GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
88GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
89GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
90GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
91GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
92GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
93GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
94GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070095GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070096
twiz@google.com0f31ca72011-03-18 17:38:11 +000097static const GrGLenum gXfermodeCoeff2Blend[] = {
98 GR_GL_ZERO,
99 GR_GL_ONE,
100 GR_GL_SRC_COLOR,
101 GR_GL_ONE_MINUS_SRC_COLOR,
102 GR_GL_DST_COLOR,
103 GR_GL_ONE_MINUS_DST_COLOR,
104 GR_GL_SRC_ALPHA,
105 GR_GL_ONE_MINUS_SRC_ALPHA,
106 GR_GL_DST_ALPHA,
107 GR_GL_ONE_MINUS_DST_ALPHA,
108 GR_GL_CONSTANT_COLOR,
109 GR_GL_ONE_MINUS_CONSTANT_COLOR,
110 GR_GL_CONSTANT_ALPHA,
111 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000112
113 // extended blend coeffs
114 GR_GL_SRC1_COLOR,
115 GR_GL_ONE_MINUS_SRC1_COLOR,
116 GR_GL_SRC1_ALPHA,
117 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000118};
119
bsalomon861e1032014-12-16 07:33:49 -0800120bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000121 static const bool gCoeffReferencesBlendConst[] = {
122 false,
123 false,
124 false,
125 false,
126 false,
127 false,
128 false,
129 false,
130 false,
131 false,
132 true,
133 true,
134 true,
135 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000136
137 // extended blend coeffs
138 false,
139 false,
140 false,
141 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000142 };
143 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700144 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000145
bsalomon@google.com47059542012-06-06 20:51:20 +0000146 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
147 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
148 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
149 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
150 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
151 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
152 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
153 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
154 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
155 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
156 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
157 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
158 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
159 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000160
bsalomon@google.com47059542012-06-06 20:51:20 +0000161 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
162 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
163 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
164 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000165
166 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700167 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000168}
169
reed@google.comac10a2d2010-12-22 21:39:39 +0000170///////////////////////////////////////////////////////////////////////////////
171
egdanielff1d5472015-09-10 08:37:20 -0700172
bsalomon682c2692015-05-22 14:01:46 -0700173GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
174 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700175 SkAutoTUnref<const GrGLInterface> glInterface(
176 reinterpret_cast<const GrGLInterface*>(backendContext));
177 if (!glInterface) {
178 glInterface.reset(GrGLDefaultInterface());
179 } else {
180 glInterface->ref();
181 }
182 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700183 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700184 }
bsalomon682c2692015-05-22 14:01:46 -0700185 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700186 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700187 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700188 }
halcanary96fcdcc2015-08-27 07:41:13 -0700189 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700190}
191
rileya@google.come38160c2012-07-03 18:03:04 +0000192static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000193
bsalomon424cc262015-05-22 10:37:30 -0700194GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000195 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700196 , fGLContext(ctx)
197 , fProgramCache(new ProgramCache(this))
198 , fHWProgramID(0)
199 , fTempSrcFBOID(0)
200 , fTempDstFBOID(0)
201 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700202 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700203 , fHWPLSEnabled(false)
204 , fPLSHasBeenUsed(false)
205 , fHWMinSampleShading(0.0) {
206 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
207 fCopyPrograms[i].fProgram = 0;
208 }
brianosman33f6b3f2016-06-02 05:49:21 -0700209 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
210 fMipmapPrograms[i].fProgram = 0;
211 }
cdaltone2e71c22016-04-07 18:13:29 -0700212 fWireRectProgram.fProgram = 0;
213 fPLSSetupProgram.fProgram = 0;
214
bsalomon424cc262015-05-22 10:37:30 -0700215 SkASSERT(ctx);
216 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000217
cdalton9c3f1432016-03-11 10:07:37 -0800218 fHWBoundTextureUniqueIDs.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000219
cdaltone2e71c22016-04-07 18:13:29 -0700220 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
221 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
222 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
223 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
224 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
225 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
226 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
227 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
228 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
229 } else {
230 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
231 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
232 }
233 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
234
cdalton74b8d322016-04-11 14:47:28 -0700235 if (this->caps()->shaderCaps()->texelBufferSupport()) {
236 fHWBufferTextures.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
237 }
238
cdaltone2e71c22016-04-07 18:13:29 -0700239 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
240 fPathRendering.reset(new GrGLPathRendering(this));
241 }
242
bsalomon424cc262015-05-22 10:37:30 -0700243 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000244 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000245 const GrGLubyte* vendor;
246 const GrGLubyte* renderer;
247 const GrGLubyte* version;
248 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
249 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
250 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800251 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000252 this);
tfarina38406c82014-10-31 07:11:12 -0700253 SkDebugf("------ VENDOR %s\n", vendor);
254 SkDebugf("------ RENDERER %s\n", renderer);
255 SkDebugf("------ VERSION %s\n", version);
256 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700257 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700258 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800259 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000260 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000261}
262
bsalomon861e1032014-12-16 07:33:49 -0800263GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700264 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
265 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800266 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700267 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700268 fMipmapProgramArrayBuffer.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700269 fWireRectArrayBuffer.reset();
270 fPLSSetupProgram.fArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800271
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000272 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000273 // detach the current program so there is no confusion on OpenGL's part
274 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000275 GL_CALL(UseProgram(0));
276 }
277
egdanield803f272015-03-18 13:01:52 -0700278 if (0 != fTempSrcFBOID) {
279 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800280 }
egdanield803f272015-03-18 13:01:52 -0700281 if (0 != fTempDstFBOID) {
282 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800283 }
egdanield803f272015-03-18 13:01:52 -0700284 if (0 != fStencilClearFBOID) {
285 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800286 }
egdaniel0f5f9672015-02-03 11:10:51 -0800287
bsalomon7ea33f52015-11-22 14:51:00 -0800288 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
289 if (0 != fCopyPrograms[i].fProgram) {
290 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
291 }
bsalomon6df86402015-06-01 10:41:49 -0700292 }
bsalomon6dea83f2015-12-03 12:58:06 -0800293
brianosman33f6b3f2016-06-02 05:49:21 -0700294 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
295 if (0 != fMipmapPrograms[i].fProgram) {
296 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
297 }
298 }
299
bsalomon6dea83f2015-12-03 12:58:06 -0800300 if (0 != fWireRectProgram.fProgram) {
301 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
302 }
303
ethannicholas22793252016-01-30 09:59:10 -0800304 if (0 != fPLSSetupProgram.fProgram) {
305 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
306 }
307
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000308 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700309}
310
cdaltone2e71c22016-04-07 18:13:29 -0700311bool GrGLGpu::createPLSSetupProgram() {
312 if (!fPLSSetupProgram.fArrayBuffer) {
313 static const GrGLfloat vdata[] = {
314 0, 0,
315 0, 1,
316 1, 0,
317 1, 1
318 };
319 fPLSSetupProgram.fArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
320 kVertex_GrBufferType,
321 kStatic_GrAccessPattern, vdata));
322 if (!fPLSSetupProgram.fArrayBuffer) {
323 return false;
324 }
325 }
326
327 SkASSERT(!fPLSSetupProgram.fProgram);
328 GL_CALL_RET(fPLSSetupProgram.fProgram, CreateProgram());
329 if (!fPLSSetupProgram.fProgram) {
330 return false;
331 }
332
cdaltonc08f1962016-02-12 12:14:06 -0800333 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
334 const char* version = glslCaps->versionDeclString();
ethannicholas22793252016-01-30 09:59:10 -0800335
336 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
337 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
338 GrShaderVar::kUniform_TypeModifier);
339 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
egdaniel990dbc82016-07-13 14:09:30 -0700340 GrGLSLShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
341 GrShaderVar::kUniform_TypeModifier);
ethannicholas22793252016-01-30 09:59:10 -0800342 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
halcanary9d524f22016-03-29 09:03:52 -0700343
ethannicholas22793252016-01-30 09:59:10 -0800344 SkString vshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800345 if (glslCaps->noperspectiveInterpolationSupport()) {
346 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
347 vshaderTxt.appendf("#extension %s : require\n", extension);
348 }
349 vTexCoord.addModifier("noperspective");
350 }
351 aVertex.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800352 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800353 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800354 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800355 uPosXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800356 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800357 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800358 vshaderTxt.append(";");
halcanary9d524f22016-03-29 09:03:52 -0700359
ethannicholas22793252016-01-30 09:59:10 -0800360 vshaderTxt.append(
361 "// PLS Setup Program VS\n"
362 "void main() {"
363 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
364 " gl_Position.zw = vec2(0, 1);"
365 "}"
366 );
367
368 SkString fshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800369 if (glslCaps->noperspectiveInterpolationSupport()) {
370 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
371 fshaderTxt.appendf("#extension %s : require\n", extension);
372 }
373 }
ethannicholas22793252016-01-30 09:59:10 -0800374 fshaderTxt.append("#extension ");
cdaltonc08f1962016-02-12 12:14:06 -0800375 fshaderTxt.append(glslCaps->fbFetchExtensionString());
ethannicholas22793252016-01-30 09:59:10 -0800376 fshaderTxt.append(" : require\n");
377 fshaderTxt.append("#extension GL_EXT_shader_pixel_local_storage : require\n");
cdaltonc08f1962016-02-12 12:14:06 -0800378 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800379 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
cdaltonc08f1962016-02-12 12:14:06 -0800380 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800381 fshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800382 uTexture.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800383 fshaderTxt.append(";");
384
385 fshaderTxt.appendf(
386 "// PLS Setup Program FS\n"
387 GR_GL_PLS_PATH_DATA_DECL
388 "void main() {\n"
389 " " GR_GL_PLS_DSTCOLOR_NAME " = gl_LastFragColorARM;\n"
390 " pls.windings = ivec4(0, 0, 0, 0);\n"
391 "}"
392 );
cdaltone2e71c22016-04-07 18:13:29 -0700393
ethannicholas22793252016-01-30 09:59:10 -0800394 const char* str;
395 GrGLint length;
396
397 str = vshaderTxt.c_str();
398 length = SkToInt(vshaderTxt.size());
399 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
400 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
401
402 str = fshaderTxt.c_str();
403 length = SkToInt(fshaderTxt.size());
404 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
405 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
406
407 GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
408
409 GL_CALL_RET(fPLSSetupProgram.fPosXformUniform, GetUniformLocation(fPLSSetupProgram.fProgram,
410 "u_posXform"));
411
412 GL_CALL(BindAttribLocation(fPLSSetupProgram.fProgram, 0, "a_vertex"));
413
414 GL_CALL(DeleteShader(vshader));
415 GL_CALL(DeleteShader(fshader));
416
cdaltone2e71c22016-04-07 18:13:29 -0700417 return true;
ethannicholas22793252016-01-30 09:59:10 -0800418}
419
bsalomon6e2aad42016-04-01 11:54:31 -0700420void GrGLGpu::disconnect(DisconnectType type) {
421 INHERITED::disconnect(type);
422 if (DisconnectType::kCleanup == type) {
423 if (fHWProgramID) {
424 GL_CALL(UseProgram(0));
425 }
426 if (fTempSrcFBOID) {
427 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
428 }
429 if (fTempDstFBOID) {
430 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
431 }
432 if (fStencilClearFBOID) {
433 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
434 }
bsalomon6e2aad42016-04-01 11:54:31 -0700435 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
436 if (fCopyPrograms[i].fProgram) {
437 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
438 }
439 }
brianosman33f6b3f2016-06-02 05:49:21 -0700440 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
441 if (fMipmapPrograms[i].fProgram) {
442 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
443 }
444 }
bsalomon6e2aad42016-04-01 11:54:31 -0700445 if (fWireRectProgram.fProgram) {
446 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
447 }
bsalomon6e2aad42016-04-01 11:54:31 -0700448 if (fPLSSetupProgram.fProgram) {
449 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
450 }
bsalomon6e2aad42016-04-01 11:54:31 -0700451 } else {
452 if (fProgramCache) {
453 fProgramCache->abandon();
454 }
455 }
456
457 delete fProgramCache;
458 fProgramCache = nullptr;
459
bsalomonc8dc1f72014-08-21 13:02:13 -0700460 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700461 fTempSrcFBOID = 0;
462 fTempDstFBOID = 0;
463 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700464 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800465 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
466 fCopyPrograms[i].fProgram = 0;
467 }
brianosman33f6b3f2016-06-02 05:49:21 -0700468 fMipmapProgramArrayBuffer.reset();
469 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
470 fMipmapPrograms[i].fProgram = 0;
471 }
bsalomon6dea83f2015-12-03 12:58:06 -0800472 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700473 fWireRectArrayBuffer.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700474 fPLSSetupProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700475 fPLSSetupProgram.fArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700476 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700477 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700478 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000479}
480
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000481///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000482
bsalomon861e1032014-12-16 07:33:49 -0800483void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000484 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000485 if (resetBits & kMisc_GrGLBackendState) {
486 GL_CALL(Disable(GR_GL_DEPTH_TEST));
487 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000488
cdaltone2e71c22016-04-07 18:13:29 -0700489 fHWBufferState[kTexel_GrBufferType].invalidate();
490 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
491 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
492 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700493
robertphillips5fa7f302016-07-21 09:21:04 -0700494 fHWDrawFace = GrDrawFace::kInvalid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000496 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000497 // Desktop-only state that we never change
498 if (!this->glCaps().isCoreProfile()) {
499 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
500 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
501 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
502 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
503 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
504 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
505 }
506 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
507 // core profile. This seems like a bug since the core spec removes any mention of
508 // GL_ARB_imaging.
509 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
510 GL_CALL(Disable(GR_GL_COLOR_TABLE));
511 }
512 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
513 // Since ES doesn't support glPointSize at all we always use the VS to
514 // set the point size
515 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
516
517 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
518 // currently part of our gl interface. There are probably others as
519 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000520 }
joshualitt58162332014-08-01 06:44:53 -0700521
522 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700523 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700524 // The arm extension requires specifically enabling MSAA fetching per sample.
525 // On some devices this may have a perf hit. Also multiple render targets are disabled
526 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
527 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000528 fHWWriteToColor = kUnknown_TriState;
529 // we only ever use lines in hairline mode
530 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700531 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000532 }
edisonn@google.comba669992013-06-28 16:03:21 +0000533
egdanielb414f252014-07-29 13:15:47 -0700534 if (resetBits & kMSAAEnable_GrGLBackendState) {
535 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700536
egdanieleed519e2016-01-15 11:36:18 -0800537 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800538 if (0 != this->caps()->maxRasterSamples()) {
539 fHWRasterMultisampleEnabled = kUnknown_TriState;
540 fHWNumRasterSamples = 0;
541 }
542
543 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
544 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700545 GL_CALL(CoverageModulation(GR_GL_RGBA));
546 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000547 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000548
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000549 fHWActiveTextureUnitIdx = -1; // invalid
550
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000551 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700552 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
553 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000554 }
cdalton74b8d322016-04-11 14:47:28 -0700555 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
556 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
557 fHWBufferTextures[b].fKnownBound = false;
558 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000559 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000560
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000561 if (resetBits & kBlend_GrGLBackendState) {
562 fHWBlendState.invalidate();
563 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000564
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000565 if (resetBits & kView_GrGLBackendState) {
566 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700567 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000568 fHWViewport.invalidate();
569 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000571 if (resetBits & kStencil_GrGLBackendState) {
572 fHWStencilSettings.invalidate();
573 fHWStencilTestEnabled = kUnknown_TriState;
574 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000575
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000576 // Vertex
577 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700578 fHWVertexArrayState.invalidate();
579 fHWBufferState[kVertex_GrBufferType].invalidate();
580 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000581 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000583 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700584 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700585 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000587
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000588 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700589 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700590 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000592 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000593
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000594 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000595 if (resetBits & kPixelStore_GrGLBackendState) {
596 if (this->glCaps().unpackRowLengthSupport()) {
597 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
598 }
599 if (this->glCaps().packRowLengthSupport()) {
600 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
601 }
602 if (this->glCaps().unpackFlipYSupport()) {
603 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
604 }
605 if (this->glCaps().packFlipYSupport()) {
606 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
607 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000608 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000609
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 if (resetBits & kProgram_GrGLBackendState) {
611 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000612 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000613}
614
egdanielcf614fd2015-04-22 13:58:58 -0700615static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000616 // By default, GrRenderTargets are GL's normal orientation so that they
617 // can be drawn to by the outside world without the client having
618 // to render upside down.
619 if (kDefault_GrSurfaceOrigin == origin) {
620 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
621 } else {
622 return origin;
623 }
624}
625
bsalomon6dc6f5f2015-06-18 09:12:16 -0700626GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
627 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800628#ifdef SK_IGNORE_GL_TEXTURE_TARGET
629 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700630 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000631 }
bsalomon091f60c2015-11-10 11:54:56 -0800632#else
633 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
634 if (!info || !info->fID) {
635 return nullptr;
636 }
637#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000638
bsalomon7ea33f52015-11-22 14:51:00 -0800639 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
640 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
641
bsalomonb15b4c12014-10-29 12:41:57 -0700642 GrGLTexture::IDDesc idDesc;
643 GrSurfaceDesc surfDesc;
644
bsalomon091f60c2015-11-10 11:54:56 -0800645#ifdef SK_IGNORE_GL_TEXTURE_TARGET
646 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
cblume55f2d2d2016-02-26 13:20:48 -0800647 // When we create the texture, we only
648 // create GL_TEXTURE_2D at the moment.
649 // External clients can do something different.
650
bsalomon091f60c2015-11-10 11:54:56 -0800651 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
652#else
653 idDesc.fInfo = *info;
654#endif
bsalomone5286e02016-01-14 09:24:09 -0800655
bsalomon7ea33f52015-11-22 14:51:00 -0800656 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
657 if (renderTarget) {
658 // This combination is not supported.
659 return nullptr;
660 }
cdalton9c3f1432016-03-11 10:07:37 -0800661 if (!this->glCaps().glslCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800662 return nullptr;
663 }
bsalomone5286e02016-01-14 09:24:09 -0800664 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
665 if (!this->glCaps().rectangleTextureSupport()) {
666 return nullptr;
667 }
668 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
669 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800670 }
bsalomone5286e02016-01-14 09:24:09 -0800671
672 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800673 // for a render buffer that resolves to the texture. We don't support MSAA textures.
674 if (desc.fSampleCnt && !renderTarget) {
675 return nullptr;
676 }
bsalomon10528f12015-10-14 12:54:52 -0700677
kkinnunen2e6055b2016-04-22 01:48:29 -0700678 if (kAdopt_GrWrapOwnership == ownership) {
679 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
680 } else {
681 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
bsalomone5286e02016-01-14 09:24:09 -0800682 }
bsalomonb15b4c12014-10-29 12:41:57 -0700683
bsalomonb15b4c12014-10-29 12:41:57 -0700684 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
685 surfDesc.fWidth = desc.fWidth;
686 surfDesc.fHeight = desc.fHeight;
687 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700688 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000689 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
690 // assuming the old behaviour, which is that backend textures are always
691 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
692 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
693 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700694 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000695 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700696 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000697 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000698
halcanary96fcdcc2015-08-27 07:41:13 -0700699 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000700 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700701 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700702 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700703 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000704 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700705 texture = GrGLTextureRenderTarget::CreateWrapped(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000706 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -0700707 texture = GrGLTexture::CreateWrapped(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000708 }
halcanary96fcdcc2015-08-27 07:41:13 -0700709 if (nullptr == texture) {
710 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000711 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000712
bsalomon@google.come269f212011-11-07 13:29:52 +0000713 return texture;
714}
715
bsalomon6dc6f5f2015-06-18 09:12:16 -0700716GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
717 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700718 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700719 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700720 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700721 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
kkinnunen2e6055b2016-04-22 01:48:29 -0700722 if (kAdopt_GrWrapOwnership == ownership) {
723 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
724 } else {
725 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
cblume61214052016-01-26 09:10:48 -0800726 }
csmartdaltonf9635992016-08-10 11:09:07 -0700727 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000728
bsalomonb15b4c12014-10-29 12:41:57 -0700729 GrSurfaceDesc desc;
730 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700731 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700732 desc.fWidth = wrapDesc.fWidth;
733 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700734 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700735 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
736
egdanielec00d942015-09-14 12:56:10 -0700737 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000738}
739
kkinnunen49c4c222016-04-01 04:50:37 -0700740GrRenderTarget* GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800741#ifdef SK_IGNORE_GL_TEXTURE_TARGET
742 if (!desc.fTextureHandle) {
743 return nullptr;
744 }
745#else
746 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
747 if (!info || !info->fID) {
748 return nullptr;
749 }
750#endif
751
jvanverthdb1adf52016-05-03 05:37:45 -0700752 GrGLTextureInfo texInfo;
ericrkf7b8b8a2016-02-24 14:49:51 -0800753 GrSurfaceDesc surfDesc;
754
755#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverthdb1adf52016-05-03 05:37:45 -0700756 texInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
ericrkf7b8b8a2016-02-24 14:49:51 -0800757 // We only support GL_TEXTURE_2D at the moment.
jvanverthdb1adf52016-05-03 05:37:45 -0700758 texInfo.fTarget = GR_GL_TEXTURE_2D;
ericrkf7b8b8a2016-02-24 14:49:51 -0800759#else
jvanverthdb1adf52016-05-03 05:37:45 -0700760 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800761#endif
762
jvanverthdb1adf52016-05-03 05:37:45 -0700763 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
764 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800765 // Only texture rectangle and texture 2d are supported. We do not check whether texture
766 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
767 // we assume the necessary support exists.
768 return nullptr;
769 }
770
ericrkf7b8b8a2016-02-24 14:49:51 -0800771 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
772 surfDesc.fWidth = desc.fWidth;
773 surfDesc.fHeight = desc.fHeight;
774 surfDesc.fConfig = desc.fConfig;
775 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
776 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
777 // assuming the old behaviour, which is that backend textures are always
778 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
779 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
780 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
781 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
782 } else {
783 surfDesc.fOrigin = desc.fOrigin;
784 }
785
786 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700787 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800788 return nullptr;
789 }
jvanverthdb1adf52016-05-03 05:37:45 -0700790 return GrGLRenderTarget::CreateWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800791}
792
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000793////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800794
bsalomonf0674512015-07-28 13:26:15 -0700795bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800796 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700797 DrawPreference* drawPreference,
798 WritePixelTempDrawInfo* tempDrawInfo) {
799 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
800 return false;
801 }
802
bsalomon6cb3cbe2015-07-30 07:34:27 -0700803 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
804 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
805 if (!dstSurface->asTexture()) {
806 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800807 } else {
808 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800809 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
810 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800811 return false;
812 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700813 }
814
bsalomon16921ec2015-07-30 15:34:56 -0700815 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
816 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
817 }
818
bsalomon6c9cd552016-01-22 07:17:34 -0800819 // Start off assuming no swizzling
820 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
821 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700822
823 // These settings we will always want if a temp draw is performed. Initially set the config
824 // to srcConfig, though that may be modified if we decide to do a R/G swap.
825 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
826 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
827 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
828 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
829 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
830 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
831
832 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
833
834 if (configsAreRBSwaps) {
835 if (!this->caps()->isConfigTexturable(srcConfig)) {
836 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
837 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800838 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
839 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700840 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
841 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700842 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
843 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800844 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
845 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700846 } else if (kGLES_GrGLStandard == this->glStandard() &&
847 this->glCaps().bgraIsInternalFormat()) {
848 // The internal format and external formats must match texture uploads so we can't
849 // swizzle while uploading when BGRA is a distinct internal format.
850 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
851 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800852 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
853 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700854 }
855 }
856
857 if (!this->glCaps().unpackFlipYSupport() &&
858 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
859 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
860 }
861
862 return true;
863}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000864
jvanverth17aa0472016-01-05 10:41:27 -0800865static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
866 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700867 if (!glTex) {
868 return false;
869 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000870
bsalomon16921ec2015-07-30 15:34:56 -0700871 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
872 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
873 return false;
874 }
875
bsalomone5286e02016-01-14 09:24:09 -0800876 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
877 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800878 return false;
879 }
880
jvanverth17aa0472016-01-05 10:41:27 -0800881 return true;
882}
883
884bool GrGLGpu::onWritePixels(GrSurface* surface,
885 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800886 GrPixelConfig config,
887 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800888 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
889
890 if (!check_write_and_transfer_input(glTex, surface, config)) {
891 return false;
892 }
893
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000894 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700895 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000896
krajcevski145d48c2014-06-11 16:07:50 -0700897 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700898 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800899 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700900 SkASSERT(config == glTex->desc().fConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800901 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800902 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700903 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800904 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800905 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700906 }
907
cblume55f2d2d2016-02-26 13:20:48 -0800908 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000909}
910
jvanverthc3d706f2016-04-20 10:33:27 -0700911bool GrGLGpu::onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800912 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700913 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800914 size_t offset, size_t rowBytes) {
jvanverthc3d706f2016-04-20 10:33:27 -0700915 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800916
jvanverthc3d706f2016-04-20 10:33:27 -0700917 if (!check_write_and_transfer_input(glTex, surface, config)) {
jvanverth17aa0472016-01-05 10:41:27 -0800918 return false;
919 }
920
921 // For the moment, can't transfer compressed data
922 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
923 return false;
924 }
925
926 this->setScratchTextureUnit();
927 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
928
cdalton397536c2016-03-25 12:15:03 -0700929 SkASSERT(!transferBuffer->isMapped());
930 SkASSERT(!transferBuffer->isCPUBacked());
931 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700932 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800933
934 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800935 GrMipLevel mipLevel;
jvanverthc3d706f2016-04-20 10:33:27 -0700936 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800937 mipLevel.fRowBytes = rowBytes;
938 SkSTArray<1, GrMipLevel> texels;
939 texels.push_back(mipLevel);
jcgregorioe7d7f902016-02-04 19:51:25 -0800940 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800941 left, top, width, height, config, texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700942 return success;
jvanverth17aa0472016-01-05 10:41:27 -0800943}
944
bsalomonf46a1242015-12-15 12:37:38 -0800945// For GL_[UN]PACK_ALIGNMENT.
946static inline GrGLint config_alignment(GrPixelConfig config) {
947 SkASSERT(!GrPixelConfigIsCompressed(config));
948 switch (config) {
949 case kAlpha_8_GrPixelConfig:
950 return 1;
951 case kRGB_565_GrPixelConfig:
952 case kRGBA_4444_GrPixelConfig:
953 case kAlpha_half_GrPixelConfig:
954 case kRGBA_half_GrPixelConfig:
955 return 2;
956 case kRGBA_8888_GrPixelConfig:
957 case kBGRA_8888_GrPixelConfig:
958 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700959 case kSBGRA_8888_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800960 case kRGBA_float_GrPixelConfig:
961 return 4;
962 default:
963 return 0;
964 }
965}
966
bsalomonb15b4c12014-10-29 12:41:57 -0700967static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
968 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700969 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000970 return GR_GL_GET_ERROR(interface);
971 } else {
972 return CHECK_ALLOC_ERROR(interface);
973 }
974}
975
cblume55f2d2d2016-02-26 13:20:48 -0800976/**
977 * Creates storage space for the texture and fills it with texels.
978 *
979 * @param desc The surface descriptor for the texture being created.
980 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800981 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -0800982 * @param internalFormat The data format used for the internal storage of the texture.
983 * @param externalFormat The data format used for the external storage of the texture.
984 * @param externalType The type of the data used for the external storage of the texture.
985 * @param texels The texel data of the texture being created.
986 * @param baseWidth The width of the texture's base mipmap level
987 * @param baseHeight The height of the texture's base mipmap level
988 * @param succeeded Set to true if allocating and populating the texture completed
989 * without error.
990 */
bsalomone699d0c2016-03-09 06:25:15 -0800991static bool allocate_and_populate_uncompressed_texture(const GrSurfaceDesc& desc,
cblume55f2d2d2016-02-26 13:20:48 -0800992 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800993 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800994 GrGLenum target,
995 GrGLenum internalFormat,
996 GrGLenum externalFormat,
997 GrGLenum externalType,
998 const SkTArray<GrMipLevel>& texels,
bsalomone699d0c2016-03-09 06:25:15 -0800999 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -08001000 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -08001001
1002 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
1003 // We can only use TexStorage if we know we will not later change the storage requirements.
1004 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1005 // Right now, we cannot know if we will later add mipmaps or not.
1006 // The only time we can use TexStorage is when we already have the
1007 // mipmaps.
1008 useTexStorage &= texels.count() > 1;
1009
1010 if (useTexStorage) {
1011 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001012 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -08001013 TexStorage2D(target,
1014 texels.count(),
1015 internalFormat,
1016 desc.fWidth, desc.fHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001017 GrGLenum error = check_alloc_error(desc, &interface);
1018 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001019 return false;
cblume790d5132016-02-29 11:13:29 -08001020 } else {
1021 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1022 const void* currentMipData = texels[currentMipLevel].fPixels;
1023 if (currentMipData == nullptr) {
1024 continue;
1025 }
1026 int twoToTheMipLevel = 1 << currentMipLevel;
1027 int currentWidth = SkTMax(1, desc.fWidth / twoToTheMipLevel);
1028 int currentHeight = SkTMax(1, desc.fHeight / twoToTheMipLevel);
1029
1030 GR_GL_CALL(&interface,
1031 TexSubImage2D(target,
1032 currentMipLevel,
1033 0, // left
1034 0, // top
1035 currentWidth,
1036 currentHeight,
1037 externalFormat, externalType,
1038 currentMipData));
1039 }
bsalomone699d0c2016-03-09 06:25:15 -08001040 return true;
cblume790d5132016-02-29 11:13:29 -08001041 }
1042 } else {
bsalomone699d0c2016-03-09 06:25:15 -08001043 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -08001044 GL_ALLOC_CALL(&interface,
1045 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001046 0,
cblume790d5132016-02-29 11:13:29 -08001047 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001048 baseWidth,
1049 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001050 0, // border
1051 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001052 nullptr));
cblume790d5132016-02-29 11:13:29 -08001053 GrGLenum error = check_alloc_error(desc, &interface);
1054 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001055 return false;
1056 }
1057 } else {
1058 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1059 int twoToTheMipLevel = 1 << currentMipLevel;
1060 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1061 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1062 const void* currentMipData = texels[currentMipLevel].fPixels;
1063 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1064 // This will allocate texture memory which we can later populate.
1065 GL_ALLOC_CALL(&interface,
1066 TexImage2D(target,
1067 currentMipLevel,
1068 internalFormat,
1069 currentWidth,
1070 currentHeight,
1071 0, // border
1072 externalFormat, externalType,
1073 currentMipData));
1074 GrGLenum error = check_alloc_error(desc, &interface);
1075 if (error != GR_GL_NO_ERROR) {
1076 return false;
1077 }
cblume790d5132016-02-29 11:13:29 -08001078 }
cblume55f2d2d2016-02-26 13:20:48 -08001079 }
1080 }
bsalomone699d0c2016-03-09 06:25:15 -08001081 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001082}
1083
1084/**
1085 * Creates storage space for the texture and fills it with texels.
1086 *
1087 * @param desc The surface descriptor for the texture being created.
1088 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -08001089 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -08001090 * @param internalFormat The data format used for the internal storage of the texture.
1091 * @param texels The texel data of the texture being created.
cblume55f2d2d2016-02-26 13:20:48 -08001092 */
1093static bool allocate_and_populate_compressed_texture(const GrSurfaceDesc& desc,
1094 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -08001095 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -08001096 GrGLenum target, GrGLenum internalFormat,
1097 const SkTArray<GrMipLevel>& texels,
1098 int baseWidth, int baseHeight) {
1099 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001100
cblume790d5132016-02-29 11:13:29 -08001101 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
1102 // We can only use TexStorage if we know we will not later change the storage requirements.
1103 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1104 // Right now, we cannot know if we will later add mipmaps or not.
1105 // The only time we can use TexStorage is when we already have the
1106 // mipmaps.
1107 useTexStorage &= texels.count() > 1;
cblume55f2d2d2016-02-26 13:20:48 -08001108
cblume790d5132016-02-29 11:13:29 -08001109 if (useTexStorage) {
1110 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001111 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -08001112 TexStorage2D(target,
1113 texels.count(),
1114 internalFormat,
1115 baseWidth, baseHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001116 GrGLenum error = check_alloc_error(desc, &interface);
1117 if (error != GR_GL_NO_ERROR) {
1118 return false;
cblume790d5132016-02-29 11:13:29 -08001119 } else {
1120 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1121 const void* currentMipData = texels[currentMipLevel].fPixels;
1122 if (currentMipData == nullptr) {
1123 continue;
1124 }
1125
1126 int twoToTheMipLevel = 1 << currentMipLevel;
1127 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1128 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1129
1130 // Make sure that the width and height that we pass to OpenGL
1131 // is a multiple of the block size.
1132 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1133 currentHeight);
1134 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1135 currentMipLevel,
1136 0, // left
1137 0, // top
1138 currentWidth,
1139 currentHeight,
1140 internalFormat,
1141 SkToInt(dataSize),
1142 currentMipData));
1143 }
1144 }
1145 } else {
1146 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1147 int twoToTheMipLevel = 1 << currentMipLevel;
1148 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1149 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1150
1151 // Make sure that the width and height that we pass to OpenGL
1152 // is a multiple of the block size.
1153 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, baseWidth, baseHeight);
1154
1155 GL_ALLOC_CALL(&interface,
1156 CompressedTexImage2D(target,
1157 currentMipLevel,
1158 internalFormat,
1159 currentWidth,
1160 currentHeight,
1161 0, // border
1162 SkToInt(dataSize),
1163 texels[currentMipLevel].fPixels));
1164
1165 GrGLenum error = check_alloc_error(desc, &interface);
1166 if (error != GR_GL_NO_ERROR) {
1167 return false;
1168 }
cblume55f2d2d2016-02-26 13:20:48 -08001169 }
1170 }
1171
1172 return true;
1173}
1174
1175/**
1176 * After a texture is created, any state which was altered during its creation
1177 * needs to be restored.
1178 *
1179 * @param interface The GL interface to use.
1180 * @param caps The capabilities of the GL device.
1181 * @param restoreGLRowLength Should the row length unpacking be restored?
1182 * @param glFlipY Did GL flip the texture vertically?
1183 */
1184static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1185 bool restoreGLRowLength, bool glFlipY) {
1186 if (restoreGLRowLength) {
1187 SkASSERT(caps.unpackRowLengthSupport());
1188 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1189 }
1190 if (glFlipY) {
1191 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1192 }
1193}
1194
bsalomon861e1032014-12-16 07:33:49 -08001195bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -08001196 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -08001197 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001198 int left, int top, int width, int height,
1199 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001200 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001201 // If we're uploading compressed data then we should be using uploadCompressedTexData
1202 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1203
bsalomon5b30c6f2015-12-17 14:17:34 -08001204 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
1205
cblume55f2d2d2016-02-26 13:20:48 -08001206 // texels is const.
1207 // But we may need to flip the texture vertically to prepare it.
1208 // Rather than flip in place and alter the incoming data,
1209 // we allocate a new buffer to flip into.
1210 // This means we need to make a non-const shallow copy of texels.
1211 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1212
1213 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1214 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -08001215 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001216 }
1217
1218 const GrGLInterface* interface = this->glInterface();
1219 const GrGLCaps& caps = this->glCaps();
1220
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001221 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001222
1223 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001224 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001225 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001226
cblume55f2d2d2016-02-26 13:20:48 -08001227 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1228 int twoToTheMipLevel = 1 << currentMipLevel;
1229 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1230 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1231
cblume55f2d2d2016-02-26 13:20:48 -08001232 if (currentHeight > SK_MaxS32 ||
1233 currentWidth > SK_MaxS32) {
1234 return false;
1235 }
1236 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
1237 &currentWidth,
1238 &currentHeight,
1239 &texelsShallowCopy[currentMipLevel].fPixels,
1240 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
1241 return false;
1242 }
1243 if (currentWidth < 0 || currentHeight < 0) {
1244 return false;
1245 }
1246 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001247
bsalomon5b30c6f2015-12-17 14:17:34 -08001248 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001249 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001250 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001251 GrGLenum externalFormat;
1252 GrGLenum externalType;
1253 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
1254 &externalFormat, &externalType)) {
1255 return false;
1256 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001257 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001258 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001259 * because our srcData has extra bytes past each row. If so, we need
1260 * to trim those off here, since GL ES may not let us specify
1261 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001262 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001263 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001264 bool swFlipY = false;
1265 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001266
bsalomone699d0c2016-03-09 06:25:15 -08001267 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001268 if (caps.unpackFlipYSupport()) {
1269 glFlipY = true;
1270 } else {
1271 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001272 }
cblume55f2d2d2016-02-26 13:20:48 -08001273 }
1274
1275 // in case we need a temporary, trimmed copy of the src pixels
1276 SkAutoSMalloc<128 * 128> tempStorage;
1277
1278 // find the combined size of all the mip levels and the relative offset of
1279 // each into the collective buffer
1280 size_t combined_buffer_size = 0;
1281 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1282 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1283 int twoToTheMipLevel = 1 << currentMipLevel;
1284 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1285 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1286 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1287 individual_mip_offsets.push_back(combined_buffer_size);
1288 combined_buffer_size += trimmedSize;
1289 }
1290 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1291
1292 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001293 int twoToTheMipLevel = 1 << currentMipLevel;
1294 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1295 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1296 const size_t trimRowBytes = currentWidth * bpp;
1297
1298 /*
1299 * check whether to allocate a temporary buffer for flipping y or
1300 * because our srcData has extra bytes past each row. If so, we need
1301 * to trim those off here, since GL ES may not let us specify
1302 * GL_UNPACK_ROW_LENGTH.
1303 */
1304 restoreGLRowLength = false;
1305
1306 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
ericrk154349b2016-05-10 14:36:53 -07001307
1308 // TODO: This optimization should be enabled with or without mips.
1309 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1310 // mip level, before calling glTexImage2D.
1311 const bool usesMips = texelsShallowCopy.count() > 1;
1312 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001313 // can't use this for flipping, only non-neg values allowed. :(
1314 if (rowBytes != trimRowBytes) {
1315 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001316 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001317 restoreGLRowLength = true;
1318 }
jvanverth17aa0472016-01-05 10:41:27 -08001319 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001320 if (trimRowBytes != rowBytes || swFlipY) {
1321 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001322 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1323 if (swFlipY && currentHeight >= 1) {
1324 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001325 }
cblume55f2d2d2016-02-26 13:20:48 -08001326 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1327 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001328 memcpy(dst, src, trimRowBytes);
1329 if (swFlipY) {
1330 src -= rowBytes;
1331 } else {
1332 src += rowBytes;
1333 }
1334 dst += trimRowBytes;
1335 }
1336 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001337 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1338 individual_mip_offsets[currentMipLevel];
1339 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001340 }
jvanverth17aa0472016-01-05 10:41:27 -08001341 } else {
1342 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001343 }
bsalomone699d0c2016-03-09 06:25:15 -08001344 }
1345
1346 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001347 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001348 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001349 }
cblume55f2d2d2016-02-26 13:20:48 -08001350 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT,
1351 config_alignment(desc.fConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001352 }
cblume55f2d2d2016-02-26 13:20:48 -08001353
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001354 bool succeeded = true;
cblume55f2d2d2016-02-26 13:20:48 -08001355 if (kNewTexture_UploadType == uploadType &&
1356 0 == left && 0 == top &&
bsalomon7e68ab72016-04-13 14:29:25 -07001357 desc.fWidth == width && desc.fHeight == height) {
bsalomone699d0c2016-03-09 06:25:15 -08001358 succeeded = allocate_and_populate_uncompressed_texture(desc, *interface, caps, target,
1359 internalFormat, externalFormat,
1360 externalType, texelsShallowCopy,
1361 width, height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001362 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001363 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001364 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001365 }
cblume55f2d2d2016-02-26 13:20:48 -08001366 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1367 currentMipLevel++) {
1368 int twoToTheMipLevel = 1 << currentMipLevel;
1369 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1370 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001371
1372 GL_CALL(TexSubImage2D(target,
1373 currentMipLevel,
1374 left, top,
1375 currentWidth,
1376 currentHeight,
1377 externalFormat, externalType,
1378 texelsShallowCopy[currentMipLevel].fPixels));
1379 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001380 }
1381
cblume55f2d2d2016-02-26 13:20:48 -08001382 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1383
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001384 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001385}
1386
krajcevski145d48c2014-06-11 16:07:50 -07001387// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001388// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001389// create a CompressedTexData struct that takes a desc/ptr and figures out
1390// the proper upload semantics. Then users can construct this function how they
1391// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -08001392bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -07001393 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -08001394 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -08001395 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001396 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -08001397 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
krajcevski9c0e6292014-06-02 07:38:14 -07001398
1399 // No support for software flip y, yet...
1400 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
1401
cblume55f2d2d2016-02-26 13:20:48 -08001402 const GrGLInterface* interface = this->glInterface();
1403 const GrGLCaps& caps = this->glCaps();
1404
krajcevski145d48c2014-06-11 16:07:50 -07001405 if (-1 == width) {
1406 width = desc.fWidth;
1407 }
1408#ifdef SK_DEBUG
1409 else {
1410 SkASSERT(width <= desc.fWidth);
1411 }
1412#endif
1413
1414 if (-1 == height) {
1415 height = desc.fHeight;
1416 }
1417#ifdef SK_DEBUG
1418 else {
1419 SkASSERT(height <= desc.fHeight);
1420 }
1421#endif
1422
bsalomon76148af2016-01-12 11:13:47 -08001423 // We only need the internal format for compressed 2D textures.
1424 GrGLenum internalFormat;
cblume55f2d2d2016-02-26 13:20:48 -08001425 if (!caps.getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001426 return false;
1427 }
krajcevski9c0e6292014-06-02 07:38:14 -07001428
jvanverth17aa0472016-01-05 10:41:27 -08001429 if (kNewTexture_UploadType == uploadType) {
cblume790d5132016-02-29 11:13:29 -08001430 return allocate_and_populate_compressed_texture(desc, *interface, caps, target,
1431 internalFormat, texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001432 } else {
bsalomond4cb9222014-08-11 14:19:09 -07001433 // Paletted textures can't be updated.
1434 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
1435 return false;
1436 }
cblume55f2d2d2016-02-26 13:20:48 -08001437 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001438 SkASSERT(texels[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001439
1440 int twoToTheMipLevel = 1 << currentMipLevel;
1441 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1442 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1443
1444 // Make sure that the width and height that we pass to OpenGL
1445 // is a multiple of the block size.
1446 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1447 currentHeight);
1448 GL_CALL(CompressedTexSubImage2D(target,
1449 currentMipLevel,
1450 left, top,
1451 currentWidth,
1452 currentHeight,
1453 internalFormat,
1454 SkToInt(dataSize),
1455 texels[currentMipLevel].fPixels));
1456 }
krajcevski145d48c2014-06-11 16:07:50 -07001457 }
krajcevski9c0e6292014-06-02 07:38:14 -07001458
bsalomond4cb9222014-08-11 14:19:09 -07001459 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001460}
1461
bsalomon424cc262015-05-22 10:37:30 -07001462static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001463 int sampleCount,
1464 GrGLenum format,
1465 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001466 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001467 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001468 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001469 case GrGLCaps::kDesktop_ARB_MSFBOType:
1470 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001471 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001472 case GrGLCaps::kES_3_0_MSFBOType:
1473 GL_ALLOC_CALL(ctx.interface(),
1474 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1475 sampleCount,
1476 format,
1477 width, height));
1478 break;
1479 case GrGLCaps::kES_Apple_MSFBOType:
1480 GL_ALLOC_CALL(ctx.interface(),
1481 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1482 sampleCount,
1483 format,
1484 width, height));
1485 break;
1486 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1487 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1488 GL_ALLOC_CALL(ctx.interface(),
1489 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1490 sampleCount,
1491 format,
1492 width, height));
1493 break;
1494 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001495 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001496 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001497 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001498 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001499}
1500
egdanielb0e1be22015-04-22 13:27:39 -07001501bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001502 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001503 GrGLRenderTarget::IDDesc* idDesc) {
1504 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001505 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001506 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001507 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001508 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1509 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001510 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511
1512 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001513
bsalomona11e5fc2015-12-18 07:59:41 -08001514 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001515
bsalomonb15b4c12014-10-29 12:41:57 -07001516 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001517 goto FAILED;
1518 }
1519
egdanield803f272015-03-18 13:01:52 -07001520 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1521 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001522 goto FAILED;
1523 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001524
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001525 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1526 // the texture bound to the other. The exception is the IMG multisample extension. With this
1527 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1528 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001529 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001530 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001531 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001532 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001533 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001534 goto FAILED;
1535 }
bsalomon76148af2016-01-12 11:13:47 -08001536 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1537 return false;
1538 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001539 } else {
egdanield803f272015-03-18 13:01:52 -07001540 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001541 }
1542
egdanield803f272015-03-18 13:01:52 -07001543 // below here we may bind the FBO
1544 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1545 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001546 SkASSERT(desc.fSampleCnt > 0);
1547 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001548 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001549 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001550 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001551 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552 goto FAILED;
1553 }
egdanield803f272015-03-18 13:01:52 -07001554 fStats.incRenderTargetBinds();
1555 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1556 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001557 GR_GL_COLOR_ATTACHMENT0,
1558 GR_GL_RENDERBUFFER,
1559 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001560 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1561 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001562 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1563 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1564 goto FAILED;
1565 }
bsalomon424cc262015-05-22 10:37:30 -07001566 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001567 }
1568 }
egdanield803f272015-03-18 13:01:52 -07001569 fStats.incRenderTargetBinds();
1570 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001571
bsalomonb15b4c12014-10-29 12:41:57 -07001572 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001573 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001574 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001575 texInfo.fTarget,
1576 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001577 } else {
egdanield803f272015-03-18 13:01:52 -07001578 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001579 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001580 texInfo.fTarget,
1581 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001582 }
bsalomonb15b4c12014-10-29 12:41:57 -07001583 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1584 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001585 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001586 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1587 goto FAILED;
1588 }
bsalomon424cc262015-05-22 10:37:30 -07001589 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001590 }
1591
1592 return true;
1593
1594FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001595 if (idDesc->fMSColorRenderbufferID) {
1596 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001597 }
egdanield803f272015-03-18 13:01:52 -07001598 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1599 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001600 }
egdanield803f272015-03-18 13:01:52 -07001601 if (idDesc->fTexFBOID) {
1602 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001603 }
1604 return false;
1605}
1606
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001607// good to set a break-point here to know when createTexture fails
1608static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001609// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001610 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001611}
1612
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001613#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001614static size_t as_size_t(int x) {
1615 return x;
1616}
1617#endif
1618
kkinnunen2e6055b2016-04-22 01:48:29 -07001619static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface) {
cblume55f2d2d2016-02-26 13:20:48 -08001620 GrGLTexture::IDDesc idDesc;
1621 idDesc.fInfo.fID = 0;
1622 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
kkinnunen2e6055b2016-04-22 01:48:29 -07001623 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
cblume55f2d2d2016-02-26 13:20:48 -08001624 // When we create the texture, we only
1625 // create GL_TEXTURE_2D at the moment.
1626 // External clients can do something different.
1627 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1628 return idDesc;
1629}
1630
1631static void set_initial_texture_params(const GrGLInterface* interface,
1632 const GrGLTextureInfo& info,
1633 GrGLTexture::TexParams* initialTexParams) {
1634 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1635 // drivers have a bug where an FBO won't be complete if it includes a
1636 // texture that is not mipmap complete (considering the filter in use).
1637 // we only set a subset here so invalidate first
1638 initialTexParams->invalidate();
1639 initialTexParams->fMinFilter = GR_GL_NEAREST;
1640 initialTexParams->fMagFilter = GR_GL_NEAREST;
1641 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1642 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1643 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1644 GR_GL_TEXTURE_MAG_FILTER,
1645 initialTexParams->fMagFilter));
1646 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1647 GR_GL_TEXTURE_MIN_FILTER,
1648 initialTexParams->fMinFilter));
1649 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1650 GR_GL_TEXTURE_WRAP_S,
1651 initialTexParams->fWrapS));
1652 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1653 GR_GL_TEXTURE_WRAP_T,
1654 initialTexParams->fWrapT));
1655}
1656
egdanielb0e1be22015-04-22 13:27:39 -07001657GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001658 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001659 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001660 // We fail if the MSAA was requested and is not available.
1661 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001662 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001663 return return_null_texture();
1664 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001665
bsalomonf2703d82014-10-28 14:33:06 -07001666 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001667
bsalomonb15b4c12014-10-29 12:41:57 -07001668 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001669 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001670 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001671 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001672 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001673 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001674
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001675 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001677 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001678 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001679 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001680
kkinnunen2e6055b2016-04-22 01:48:29 -07001681 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001682 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 return return_null_texture();
1684 }
kkinnunen2e6055b2016-04-22 01:48:29 -07001685 tex = new GrGLTextureRenderTarget(this, budgeted, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001686 } else {
cblume55f2d2d2016-02-26 13:20:48 -08001687 bool wasMipMapDataProvided = false;
1688 if (texels.count() > 1) {
1689 wasMipMapDataProvided = true;
1690 }
kkinnunen2e6055b2016-04-22 01:48:29 -07001691 tex = new GrGLTexture(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001693 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001694#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001695 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001696 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001697#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001698 return tex;
1699}
1700
egdanielb0e1be22015-04-22 13:27:39 -07001701GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001702 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001703 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001704 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001705 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001706 return return_null_texture();
1707 }
1708
kkinnunen2e6055b2016-04-22 01:48:29 -07001709 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface());
bsalomon091f60c2015-11-10 11:54:56 -08001710 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001711 return return_null_texture();
1712 }
1713
1714 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001715 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001716
krajcevski9c0e6292014-06-02 07:38:14 -07001717 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001718 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001719
cblume55f2d2d2016-02-26 13:20:48 -08001720 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001721 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001722 return return_null_texture();
1723 }
1724
1725 GrGLTexture* tex;
kkinnunen2e6055b2016-04-22 01:48:29 -07001726 tex = new GrGLTexture(this, budgeted, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001727 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1728#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001729 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001730 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001731#endif
1732 return tex;
1733}
1734
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001735namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001736
egdaniel8dc7c3a2015-04-16 11:22:42 -07001737const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001738
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001739void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001740 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001741
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001742 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001743 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001744 (kUnknownBitCount == format->fTotalBits));
1745 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001746 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001747 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1748 (GrGLint*)&format->fStencilBits);
1749 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001750 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001751 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1752 (GrGLint*)&format->fTotalBits);
1753 format->fTotalBits += format->fStencilBits;
1754 } else {
1755 format->fTotalBits = format->fStencilBits;
1756 }
1757 }
1758}
1759}
1760
egdanielff1d5472015-09-10 08:37:20 -07001761int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001762 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001763 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001764 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1765 // Default to unsupported, set this if we find a stencil format that works.
1766 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001767 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001768 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001769 GL_CALL(GenTextures(1, &colorID));
1770 this->setScratchTextureUnit();
1771 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1772 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1773 GR_GL_TEXTURE_MAG_FILTER,
1774 GR_GL_NEAREST));
1775 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1776 GR_GL_TEXTURE_MIN_FILTER,
1777 GR_GL_NEAREST));
1778 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1779 GR_GL_TEXTURE_WRAP_S,
1780 GR_GL_CLAMP_TO_EDGE));
1781 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1782 GR_GL_TEXTURE_WRAP_T,
1783 GR_GL_CLAMP_TO_EDGE));
1784
bsalomon76148af2016-01-12 11:13:47 -08001785 GrGLenum internalFormat;
1786 GrGLenum externalFormat;
1787 GrGLenum externalType;
1788 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1789 &externalType)) {
1790 return false;
1791 }
egdanielff1d5472015-09-10 08:37:20 -07001792 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1793 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001794 0,
bsalomon76148af2016-01-12 11:13:47 -08001795 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001796 kSize,
1797 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001798 0,
bsalomon76148af2016-01-12 11:13:47 -08001799 externalFormat,
1800 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001801 NULL));
bsalomon30447372015-12-21 09:03:05 -08001802 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001803 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001804 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001805 }
1806
1807 // unbind the texture from the texture unit before binding it to the frame buffer
1808 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1809
1810 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001811 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001812 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001813 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001814 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1815 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1816 GR_GL_COLOR_ATTACHMENT0,
1817 GR_GL_TEXTURE_2D,
1818 colorID,
1819 0));
bsalomon30447372015-12-21 09:03:05 -08001820 GrGLuint sbRBID = 0;
1821 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001822
1823 // look over formats till I find a compatible one
1824 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001825 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001826 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001827 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1828 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1829 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1830 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1831 sFmt.fInternalFormat,
1832 kSize, kSize));
1833 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001834 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001835 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001836 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001837 if (sFmt.fPacked) {
1838 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1839 GR_GL_DEPTH_ATTACHMENT,
1840 GR_GL_RENDERBUFFER, sbRBID));
1841 } else {
1842 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1843 GR_GL_DEPTH_ATTACHMENT,
1844 GR_GL_RENDERBUFFER, 0));
1845 }
1846 GrGLenum status;
1847 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1848 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1849 firstWorkingStencilFormatIndex = i;
1850 break;
1851 }
egdanielff1d5472015-09-10 08:37:20 -07001852 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1853 GR_GL_STENCIL_ATTACHMENT,
1854 GR_GL_RENDERBUFFER, 0));
1855 if (sFmt.fPacked) {
1856 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1857 GR_GL_DEPTH_ATTACHMENT,
1858 GR_GL_RENDERBUFFER, 0));
1859 }
egdanielff1d5472015-09-10 08:37:20 -07001860 }
1861 }
bsalomon30447372015-12-21 09:03:05 -08001862 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001863 }
1864 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001865 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1866 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001867 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001868 }
bsalomon30447372015-12-21 09:03:05 -08001869 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001870}
1871
erikchen9a1ed5d2016-02-10 16:32:34 -08001872bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001873 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1874 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001875 info->fID = 0;
1876 info->fTarget = GR_GL_TEXTURE_2D;
1877 GL_CALL(GenTextures(1, &(info->fID)));
1878
1879 if (!info->fID) {
1880 return false;
1881 }
1882
1883 this->setScratchTextureUnit();
1884 GL_CALL(BindTexture(info->fTarget, info->fID));
1885
1886 if (renderTarget && this->glCaps().textureUsageSupport()) {
1887 // provides a hint about how this texture will be used
1888 GL_CALL(TexParameteri(info->fTarget,
1889 GR_GL_TEXTURE_USAGE,
1890 GR_GL_FRAMEBUFFER_ATTACHMENT));
1891 }
1892
cblume55f2d2d2016-02-26 13:20:48 -08001893 if (info) {
1894 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1895 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001896 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1897 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001898 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001899 GL_CALL(DeleteTextures(1, &(info->fID)));
1900 return false;
1901 }
1902 return true;
1903}
1904
egdanielec00d942015-09-14 12:56:10 -07001905GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1906 int width,
1907 int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001908 SkASSERT(width >= rt->width());
1909 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001910
vbuzinovdded6962015-06-12 08:59:45 -07001911 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001912 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001913
egdanielff1d5472015-09-10 08:37:20 -07001914 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001915 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001916 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001917 }
egdanielff1d5472015-09-10 08:37:20 -07001918
1919 if (!sbDesc.fRenderbufferID) {
1920 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1921 }
1922 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001923 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001924 }
1925 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1926 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1927 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1928 // we do this "if" so that we don't call the multisample
1929 // version on a GL that doesn't have an MSAA extension.
1930 if (samples > 0) {
1931 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1932 samples,
1933 sFmt.fInternalFormat,
1934 width, height));
1935 } else {
1936 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1937 sFmt.fInternalFormat,
1938 width, height));
1939 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1940 }
1941 fStats.incStencilAttachmentCreates();
1942 // After sized formats we attempt an unsized format and take
1943 // whatever sizes GL gives us. In that case we query for the size.
1944 GrGLStencilAttachment::Format format = sFmt;
1945 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001946 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1947 sbDesc,
1948 width,
1949 height,
1950 samples,
1951 format);
1952 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001953}
1954
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001955////////////////////////////////////////////////////////////////////////////////
1956
jvanverth73063dc2015-12-03 09:15:47 -08001957// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1958// objects are implemented as client-side-arrays on tile-deferred architectures.
1959#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1960
cdaltone2e71c22016-04-07 18:13:29 -07001961GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001962 GrAccessPattern accessPattern, const void* data) {
1963 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001964}
1965
csmartdaltone0d36292016-07-29 08:14:20 -07001966InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1967 return new GLInstancedRendering(this);
1968}
1969
bsalomon3e791242014-12-17 13:43:13 -08001970void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001971 const GrGLIRect& rtViewport,
1972 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001973 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001974 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001975 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001976 scissorState.rect().fLeft,
1977 scissorState.rect().fTop,
1978 scissorState.rect().width(),
1979 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001980 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001981 // if the scissor fully contains the viewport then we fall through and
1982 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001983 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001984 if (fHWScissorSettings.fRect != scissor) {
1985 scissor.pushToGLScissor(this->glInterface());
1986 fHWScissorSettings.fRect = scissor;
1987 }
1988 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1989 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1990 fHWScissorSettings.fEnabled = kYes_TriState;
1991 }
1992 return;
1993 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001994 }
joshualitt77b13072014-10-27 14:51:01 -07001995
1996 // See fall through note above
1997 this->disableScissor();
1998}
1999
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002000void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
2001 const GrGLRenderTarget* rt) {
2002 typedef GrWindowRectsState::Mode Mode;
2003 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
2004 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07002005
2006 if (!this->caps()->maxWindowRectangles() ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002007 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07002008 return;
csmartdalton28341fa2016-08-17 10:00:21 -07002009 }
2010
csmartdalton7535f412016-08-23 06:51:00 -07002011 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
2012 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002013 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
2014 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07002015
csmartdalton28341fa2016-08-17 10:00:21 -07002016 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002017 const SkIRect* skwindows = windowState.windows().data();
2018 int dx = -windowState.origin().x(), dy = -windowState.origin().y();
csmartdalton7535f412016-08-23 06:51:00 -07002019 for (int i = 0; i < numWindows; ++i) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002020 const SkIRect& skwindow = skwindows[i].makeOffset(dx, dy);
2021 glwindows[i].setRelativeTo(rt->getViewport(), skwindow, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002022 }
2023
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002024 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07002025 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07002026
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002027 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
csmartdalton28341fa2016-08-17 10:00:21 -07002028}
2029
2030void GrGLGpu::disableWindowRectangles() {
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002031 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07002032 return;
2033 }
2034 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002035 fHWWindowRectsState.setDisabled();
csmartdalton28341fa2016-08-17 10:00:21 -07002036}
2037
ethannicholas28ef4452016-03-25 09:26:03 -07002038void GrGLGpu::flushMinSampleShading(float minSampleShading) {
2039 if (fHWMinSampleShading != minSampleShading) {
2040 if (minSampleShading > 0.0) {
2041 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
2042 GL_CALL(MinSampleShading(minSampleShading));
2043 }
2044 else {
2045 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
2046 }
2047 fHWMinSampleShading = minSampleShading;
2048 }
2049}
2050
egdaniel0e1853c2016-03-17 11:35:45 -07002051bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc) {
brianosman33f6b3f2016-06-02 05:49:21 -07002052 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc));
2053 if (!program) {
2054 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2055 return false;
2056 }
2057
2058 program->generateMipmaps(primProc, pipeline);
2059
egdaniel080e6732014-12-22 07:35:52 -08002060 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002061 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002062
egdaniel080e6732014-12-22 07:35:52 -08002063 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08002064 this->flushDrawFace(pipeline.getDrawFace());
ethannicholas28ef4452016-03-25 09:26:03 -07002065 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08002066
bsalomon6df86402015-06-01 10:41:49 -07002067 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08002068 if (fHWProgramID != programID) {
2069 GL_CALL(UseProgram(programID));
2070 fHWProgramID = programID;
2071 }
2072
egdanield803f272015-03-18 13:01:52 -07002073 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08002074 // Swizzle the blend to match what the shader will output.
2075 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07002076 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08002077 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07002078 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002079
cdalton74b8d322016-04-11 14:47:28 -07002080 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08002081
brianosman898235c2016-04-06 07:38:23 -07002082 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -08002083 this->flushStencil(pipeline.getStencil());
2084 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002085 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002086 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !pipeline.getStencil().isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08002087
2088 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002089 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07002090 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08002091
2092 return true;
2093}
2094
joshualitt873ad0e2015-01-20 09:08:51 -08002095void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
egdaniel0e1853c2016-03-17 11:35:45 -07002096 const GrNonInstancedMesh& mesh,
bsalomonbc3d0de2014-12-15 13:45:03 -08002097 size_t* indexOffsetInBytes) {
csmartdalton485a1202016-07-13 10:16:32 -07002098 const GrBuffer* vbuf = mesh.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08002099 SkASSERT(vbuf);
2100 SkASSERT(!vbuf->isMapped());
2101
cdaltone2e71c22016-04-07 18:13:29 -07002102 GrGLAttribArrayState* attribState;
egdaniel0e1853c2016-03-17 11:35:45 -07002103 if (mesh.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002104 SkASSERT(indexOffsetInBytes);
2105
2106 *indexOffsetInBytes = 0;
csmartdalton485a1202016-07-13 10:16:32 -07002107 const GrBuffer* ibuf = mesh.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08002108 SkASSERT(ibuf);
2109 SkASSERT(!ibuf->isMapped());
2110 *indexOffsetInBytes += ibuf->baseOffset();
cdaltone2e71c22016-04-07 18:13:29 -07002111 attribState = fHWVertexArrayState.bindInternalVertexArray(this, ibuf);
2112 } else {
2113 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002114 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002115
joshualitt873ad0e2015-01-20 09:08:51 -08002116 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08002117 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002118
joshualitt873ad0e2015-01-20 09:08:51 -08002119 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08002120
egdaniel0e1853c2016-03-17 11:35:45 -07002121 size_t vertexOffsetInBytes = stride * mesh.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08002122
2123 vertexOffsetInBytes += vbuf->baseOffset();
2124
bsalomonbc3d0de2014-12-15 13:45:03 -08002125 uint32_t usedAttribArraysMask = 0;
2126 size_t offset = 0;
2127
2128 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08002129 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08002130 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08002131 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08002132 attribState->set(this,
2133 attribIndex,
cdaltone2e71c22016-04-07 18:13:29 -07002134 vbuf,
cdalton793dc262016-02-08 10:11:47 -08002135 attribType,
bsalomonbc3d0de2014-12-15 13:45:03 -08002136 stride,
2137 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08002138 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002139 }
2140 attribState->disableUnusedArrays(this, usedAttribArraysMask);
2141 }
2142}
2143
csmartdalton485a1202016-07-13 10:16:32 -07002144GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002145 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002146
cdaltone2e71c22016-04-07 18:13:29 -07002147 // Index buffer state is tied to the vertex array.
2148 if (kIndex_GrBufferType == type) {
2149 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002150 }
cdaltone2e71c22016-04-07 18:13:29 -07002151
2152 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2153 auto& bufferState = fHWBufferState[type];
2154
robertphillips8abb3702016-08-31 14:04:06 -07002155 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07002156 if (buffer->isCPUBacked()) {
2157 if (!bufferState.fBufferZeroKnownBound) {
2158 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
2159 }
2160 } else {
2161 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2162 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07002163 }
csmartdalton485a1202016-07-13 10:16:32 -07002164 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07002165 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002166 }
2167
2168 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002169}
2170
cdalton74b8d322016-04-11 14:47:28 -07002171void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
2172 if (buffer->hasAttachedToTexture()) {
2173 // Detach this buffer from any textures to ensure the underlying memory is freed.
robertphillips8abb3702016-08-31 14:04:06 -07002174 uint32_t uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07002175 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
2176 auto& buffTex = fHWBufferTextures[i];
2177 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
2178 continue;
2179 }
2180 if (i == fHWMaxUsedBufferTextureUnit) {
2181 --fHWMaxUsedBufferTextureUnit;
2182 }
2183
2184 this->setTextureUnit(i);
2185 if (!buffTex.fKnownBound) {
2186 SkASSERT(buffTex.fTextureID);
2187 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
2188 buffTex.fKnownBound = true;
2189 }
2190 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
2191 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
2192 }
2193 }
2194}
2195
bsalomon861e1032014-12-16 07:33:49 -08002196void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002197 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002198 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002199 fHWScissorSettings.fEnabled = kNo_TriState;
2200 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 }
2202}
2203
csmartdalton29df7602016-08-31 11:55:52 -07002204void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07002205 this->handleDirtyContext();
2206
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002207 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002208 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002209 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002210
csmartdalton29df7602016-08-31 11:55:52 -07002211 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
2212 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002213 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00002214
2215 GrGLfloat r, g, b, a;
2216 static const GrGLfloat scale255 = 1.f / 255.f;
2217 a = GrColorUnpackA(color) * scale255;
2218 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002219 r = GrColorUnpackR(color) * scaleRGB;
2220 g = GrColorUnpackG(color) * scaleRGB;
2221 b = GrColorUnpackB(color) * scaleRGB;
2222
2223 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002224 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002225 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002227}
2228
bsalomon861e1032014-12-16 07:33:49 -08002229void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002230 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002231 return;
2232 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002233 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002234 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002235
joshualitt77b13072014-10-27 14:51:01 -07002236 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002237 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002238
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002239 GL_CALL(StencilMask(0xffffffff));
2240 GL_CALL(ClearStencil(0));
2241 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002242 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002243}
2244
csmartdalton29df7602016-08-31 11:55:52 -07002245void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2246 bool insideStencilMask,
2247 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07002248 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002249 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002250
egdaniel8dc7c3a2015-04-16 11:22:42 -07002251 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002252 // this should only be called internally when we know we have a
2253 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002254 SkASSERT(sb);
2255 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002256#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002257 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002258 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002259#else
2260 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002261 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002262 // turned into draws. Our contract on GrDrawTarget says that
2263 // changing the clip between stencil passes may or may not
2264 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002265 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002266#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002267 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002268 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002269 value = (1 << (stencilBitCount - 1));
2270 } else {
2271 value = 0;
2272 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002273 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002274 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002275
csmartdalton29df7602016-08-31 11:55:52 -07002276 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002277 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002278
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002279 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002280 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002281 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002282 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002283}
2284
bsalomon39826022015-07-23 08:07:21 -07002285static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
2286 int width, int height, GrPixelConfig config,
2287 size_t rowBytes) {
2288 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002289 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
2290 return false;
2291 }
2292
bsalomon494aa592015-07-23 11:45:02 -07002293 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002294 static const int kMinSize = 32;
2295 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002296 return false;
2297 }
2298
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002299 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002300 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002301 return false;
2302 }
2303
2304 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002305 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002306 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2307 // clipped.
2308 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2309}
2310
bsalomon1aa20292016-01-22 08:16:09 -08002311bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
2312 auto bindRenderTarget = [this, target]() -> bool {
2313 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2314 return true;
2315 };
2316 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2317 GR_GL_GetIntegerv(this->glInterface(), query, value);
2318 };
2319 GrPixelConfig rtConfig = target->config();
2320 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2321}
2322
2323bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
2324 auto bindRenderTarget = [this, rtConfig]() -> bool {
2325 GrTextureDesc desc;
2326 desc.fConfig = rtConfig;
2327 desc.fWidth = desc.fHeight = 16;
2328 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomone699d0c2016-03-09 06:25:15 -08002329 SkAutoTUnref<GrTexture> temp(this->createTexture(desc,
2330 SkBudgeted::kNo));
bsalomon1aa20292016-01-22 08:16:09 -08002331 if (!temp) {
2332 return false;
2333 }
2334 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2335 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2336 return true;
2337 };
2338 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2339 GR_GL_GetIntegerv(this->glInterface(), query, value);
2340 };
2341 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2342}
2343
2344bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2345 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2346 return this->readPixelsSupported(rt, readConfig);
2347 } else {
2348 GrPixelConfig config = surfaceForConfig->config();
2349 return this->readPixelsSupported(config, readConfig);
2350 }
2351}
2352
bsalomone9573312016-01-25 14:33:25 -08002353static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2354 if (GrPixelConfigIsSRGB(a)) {
2355 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2356 } else if (GrPixelConfigIsSRGB(b)) {
2357 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2358 }
2359 return false;
2360}
2361
bsalomonf0674512015-07-28 13:26:15 -07002362bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2363 GrPixelConfig readConfig, DrawPreference* drawPreference,
2364 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002365 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002366
bsalomone9573312016-01-25 14:33:25 -08002367 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002368 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2369 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2370 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2371 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2372 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002373 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2374 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002375 // For now assume no swizzling, we may change that below.
2376 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2377
2378 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2379 // from will be srcConfig and we will read readConfig pixels from it.
2380 // Not that if we require a draw and return a non-renderable format for the temp surface the
2381 // base class will fail for us.
2382 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2383 tempDrawInfo->fReadConfig = readConfig;
2384
2385 if (requires_srgb_conversion(srcConfig, readConfig)) {
2386 if (!this->readPixelsSupported(readConfig, readConfig)) {
2387 return false;
2388 }
2389 // Draw to do srgb to linear conversion or vice versa.
2390 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2391 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2392 tempDrawInfo->fReadConfig = readConfig;
2393 return true;
2394 }
2395
2396 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
2397 if (!srcAsRT) {
2398 // For now keep assuming the draw is not a format transformation, just a draw to get to a
2399 // RT. We may add additional transformations below.
2400 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2401 }
bsalomon1aa20292016-01-22 08:16:09 -08002402 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2403 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002404 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002405 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2406 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002407 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002408 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002409 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002410 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2411 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002412 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002413 // Better to do a draw with a R/B swap and then read as the original config.
2414 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002415 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2416 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002417 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002418 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2419 if (readConfig == kBGRA_8888_GrPixelConfig &&
2420 this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
2421 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002422 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2423 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2424 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002425 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2426 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2427 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2428 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002429 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
2430 this->glCaps().isConfigRenderable(kSRGBA_8888_GrPixelConfig, false) &&
2431 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2432 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2433 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2434 // will effectively be sBGRA).
2435 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2436 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2437 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2438 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002439 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2440 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2441 // it's unsupported, but 32bit RGBA reads are supported.
2442 // Don't attempt to do any srgb conversions since we only care about alpha.
2443 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2444 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2445 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2446 }
2447 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2448 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2449 // first and then onReadPixels will read that to a 32bit temporary buffer.
2450 if (this->caps()->isConfigRenderable(cpuTempConfig, false)) {
2451 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2452 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2453 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2454 } else {
2455 return false;
2456 }
2457 } else {
2458 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2459 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2460 }
bsalomone6d665e2016-03-10 07:22:25 -08002461 } else if (this->caps()->isConfigRenderable(readConfig, false) &&
2462 this->readPixelsSupported(readConfig, readConfig)) {
2463 // Do a draw to convert from the src config to the read config.
2464 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2465 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2466 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002467 } else {
2468 return false;
2469 }
bsalomon39826022015-07-23 08:07:21 -07002470 }
2471
bsalomon1aa20292016-01-22 08:16:09 -08002472 if (srcAsRT &&
2473 read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002474 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002475 }
2476
bsalomon39826022015-07-23 08:07:21 -07002477 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002478}
2479
bsalomon6cb3cbe2015-07-30 07:34:27 -07002480bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002481 int left, int top,
2482 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002483 GrPixelConfig config,
2484 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002485 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002486 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002487
bsalomone9573312016-01-25 14:33:25 -08002488 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2489 if (!renderTarget) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002490 return false;
2491 }
2492
bsalomon16921ec2015-07-30 15:34:56 -07002493 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002494 if (requires_srgb_conversion(surface->config(), config)) {
2495 return false;
2496 }
2497
2498 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2499 // bit channels as RGBA and then extract A.
2500 if (!this->readPixelsSupported(renderTarget, config)) {
2501 // Don't attempt to do any srgb conversions since we only care about alpha.
2502 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
2503 if (GrPixelConfigIsSRGB(renderTarget->config())) {
2504 tempConfig = kSRGBA_8888_GrPixelConfig;
2505 }
2506 if (kAlpha_8_GrPixelConfig == config &&
2507 this->readPixelsSupported(renderTarget, tempConfig)) {
2508 SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
2509 if (this->onReadPixels(renderTarget, left, top, width, height, tempConfig, temp.get(),
2510 width*4)) {
2511 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2512 for (int j = 0; j < height; ++j) {
2513 for (int i = 0; i < width; ++i) {
2514 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2515 }
2516 }
2517 return true;
2518 }
2519 }
bsalomon16921ec2015-07-30 15:34:56 -07002520 return false;
2521 }
2522
bsalomon76148af2016-01-12 11:13:47 -08002523 GrGLenum externalFormat;
2524 GrGLenum externalType;
bsalomone9573312016-01-25 14:33:25 -08002525 if (!this->glCaps().getReadPixelsFormat(renderTarget->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002526 &externalType)) {
2527 return false;
2528 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002529 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002530
bsalomon@google.comc6980972011-11-02 19:57:21 +00002531 // resolve the render target if necessary
bsalomone9573312016-01-25 14:33:25 -08002532 switch (renderTarget->getResolveType()) {
egdanield803f272015-03-18 13:01:52 -07002533 case GrGLRenderTarget::kCantResolve_ResolveType:
2534 return false;
2535 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002536 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07002537 break;
2538 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002539 this->onResolveRenderTarget(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002540 // we don't track the state of the READ FBO ID.
2541 fStats.incRenderTargetBinds();
bsalomone9573312016-01-25 14:33:25 -08002542 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
egdanield803f272015-03-18 13:01:52 -07002543 break;
2544 default:
2545 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00002546 }
2547
bsalomone9573312016-01-25 14:33:25 -08002548 const GrGLIRect& glvp = renderTarget->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002549
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002550 // the read rect is viewport-relative
2551 GrGLIRect readRect;
bsalomone9573312016-01-25 14:33:25 -08002552 readRect.setRelativeTo(glvp, left, top, width, height, renderTarget->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002553
bsalomon9d02b262016-02-01 12:49:30 -08002554 size_t bytesPerPixel = GrBytesPerPixel(config);
2555 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002556
bsalomon@google.comc6980972011-11-02 19:57:21 +00002557 size_t readDstRowBytes = tightRowBytes;
2558 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002559
bsalomon@google.comc6980972011-11-02 19:57:21 +00002560 // determine if GL can read using the passed rowBytes or if we need
2561 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002562 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002563 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002564 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002565 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002566 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002567 readDstRowBytes = rowBytes;
2568 } else {
2569 scratch.reset(tightRowBytes * height);
2570 readDst = scratch.get();
2571 }
2572 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002573 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002574 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2575 }
bsalomonf46a1242015-12-15 12:37:38 -08002576 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2577
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002578 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2579 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002580 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002581 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002582 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002583 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2584 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002585 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002586 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002587 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002588 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002589
2590 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002591 // API presents top-to-bottom. We must preserve the padding contents. Note
2592 // that the above readPixels did not overwrite the padding.
2593 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002594 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002595 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002596 scratch.reset(tightRowBytes);
2597 void* tmpRow = scratch.get();
2598 // flip y in-place by rows
2599 const int halfY = height >> 1;
2600 char* top = reinterpret_cast<char*>(buffer);
2601 char* bottom = top + (height - 1) * rowBytes;
2602 for (int y = 0; y < halfY; y++) {
2603 memcpy(tmpRow, top, tightRowBytes);
2604 memcpy(top, bottom, tightRowBytes);
2605 memcpy(bottom, tmpRow, tightRowBytes);
2606 top += rowBytes;
2607 bottom -= rowBytes;
2608 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002609 }
2610 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002611 SkASSERT(readDst != buffer);
2612 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002613 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002614 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002615 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002616 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002617 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002618 dst += (height-1) * rowBytes;
2619 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002620 for (int y = 0; y < height; y++) {
2621 memcpy(dst, src, tightRowBytes);
2622 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002623 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002624 dst += rowBytes;
2625 } else {
2626 dst -= rowBytes;
2627 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002628 }
2629 }
2630 return true;
2631}
2632
egdaniel9cb63402016-06-23 08:37:05 -07002633GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
2634 GrRenderTarget* target,
2635 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2636 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
egdaniel066df7c2016-06-08 14:02:27 -07002637 return new GrGLGpuCommandBuffer(this);
2638}
2639
jvanverthd2d2eb92016-02-17 14:04:46 -08002640void GrGLGpu::finishDrawTarget() {
ethannicholas22793252016-01-30 09:59:10 -08002641 if (fPLSHasBeenUsed) {
halcanary9d524f22016-03-29 09:03:52 -07002642 /* There is an ARM driver bug where if we use PLS, and then draw a frame which does not
2643 * use PLS, it leaves garbage all over the place. As a workaround, we use PLS in a
ethannicholas22793252016-01-30 09:59:10 -08002644 * trivial way every frame. And since we use it every frame, there's never a point at which
2645 * it becomes safe to stop using this workaround once we start.
2646 */
2647 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002648 this->disableWindowRectangles();
ethannicholas22793252016-01-30 09:59:10 -08002649 // using PLS in the presence of MSAA results in GL_INVALID_OPERATION
cdaltonaf8bc7d2016-02-05 09:35:20 -08002650 this->flushHWAAState(nullptr, false, false);
ethannicholas22793252016-01-30 09:59:10 -08002651 SkASSERT(!fHWPLSEnabled);
2652 SkASSERT(fMSAAEnabled != kYes_TriState);
2653 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
cdaltone2e71c22016-04-07 18:13:29 -07002654 this->stampPLSSetupRect(SkRect::MakeXYWH(-100.0f, -100.0f, 0.01f, 0.01f));
ethannicholas22793252016-01-30 09:59:10 -08002655 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2656 }
2657}
ethannicholas5366a092016-01-22 09:45:47 -08002658
brianosman64d094d2016-03-25 06:01:59 -07002659void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002660 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002661
robertphillips8abb3702016-08-31 14:04:06 -07002662 uint32_t rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002663 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002664 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002665 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2666#ifdef SK_DEBUG
2667 // don't do this check in Chromium -- this is causing
2668 // lots of repeated command buffer flushes when the compositor is
2669 // rendering with Ganesh, which is really slow; even too slow for
2670 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002671 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002672 GrGLenum status;
2673 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2674 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2675 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2676 }
bsalomon160f24c2015-03-17 15:55:42 -07002677 }
egdanield803f272015-03-18 13:01:52 -07002678#endif
2679 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002680 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002681 }
2682
brianosman35b784d2016-05-05 11:52:53 -07002683 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002684 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002685 }
brianosman64d094d2016-03-25 06:01:59 -07002686
bsalomon083617b2016-02-12 12:10:14 -08002687 this->didWriteToSurface(target, bounds);
2688}
bsalomona9909122016-01-23 10:41:40 -08002689
brianosman33f6b3f2016-06-02 05:49:21 -07002690void GrGLGpu::flushFramebufferSRGB(bool enable) {
2691 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2692 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2693 fHWSRGBFramebuffer = kYes_TriState;
2694 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2695 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2696 fHWSRGBFramebuffer = kNo_TriState;
2697 }
2698}
2699
bsalomon083617b2016-02-12 12:10:14 -08002700void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2701 if (fHWViewport != viewport) {
2702 viewport.pushToGLViewport(this->glInterface());
2703 fHWViewport = viewport;
2704 }
2705}
2706
twiz@google.com0f31ca72011-03-18 17:38:11 +00002707GrGLenum gPrimitiveType2GLMode[] = {
2708 GR_GL_TRIANGLES,
2709 GR_GL_TRIANGLE_STRIP,
2710 GR_GL_TRIANGLE_FAN,
2711 GR_GL_POINTS,
2712 GR_GL_LINES,
2713 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002714};
2715
bsalomon@google.comd302f142011-03-03 13:54:13 +00002716#define SWAP_PER_DRAW 0
2717
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002718#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002719 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002720 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002721 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002722 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002723 void SwapBuf() {
2724 DWORD procID = GetCurrentProcessId();
2725 HWND hwnd = GetTopWindow(GetDesktopWindow());
2726 while(hwnd) {
2727 DWORD wndProcID = 0;
2728 GetWindowThreadProcessId(hwnd, &wndProcID);
2729 if(wndProcID == procID) {
2730 SwapBuffers(GetDC(hwnd));
2731 }
2732 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2733 }
2734 }
2735 #endif
2736#endif
2737
egdaniel9cb63402016-06-23 08:37:05 -07002738void GrGLGpu::draw(const GrPipeline& pipeline,
2739 const GrPrimitiveProcessor& primProc,
2740 const GrMesh* meshes,
2741 int meshCount) {
2742 this->handleDirtyContext();
2743
egdaniel0e1853c2016-03-17 11:35:45 -07002744 if (!this->flushGLState(pipeline, primProc)) {
bsalomond95263c2014-12-16 13:05:12 -08002745 return;
2746 }
egdaniel0e1853c2016-03-17 11:35:45 -07002747 GrPixelLocalStorageState plsState = primProc.getPixelLocalStorageState();
2748 if (!fHWPLSEnabled && plsState !=
ethannicholas22793252016-01-30 09:59:10 -08002749 GrPixelLocalStorageState::kDisabled_GrPixelLocalStorageState) {
2750 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
egdaniel0e1853c2016-03-17 11:35:45 -07002751 this->setupPixelLocalStorage(pipeline, primProc);
ethannicholas22793252016-01-30 09:59:10 -08002752 fHWPLSEnabled = true;
2753 }
2754 if (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2755 GrStencilSettings stencil;
2756 stencil.setDisabled();
2757 this->flushStencil(stencil);
2758 }
2759
egdaniel0e1853c2016-03-17 11:35:45 -07002760 for (int i = 0; i < meshCount; ++i) {
2761 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2762 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2763 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002764
egdaniel0e1853c2016-03-17 11:35:45 -07002765 const GrMesh& mesh = meshes[i];
2766 GrMesh::Iterator iter;
bsalomonfc9527a2016-08-29 09:18:39 -07002767 const GrNonInstancedMesh* nonInstMesh = iter.init(mesh);
egdaniel0e1853c2016-03-17 11:35:45 -07002768 do {
2769 size_t indexOffsetInBytes = 0;
bsalomonfc9527a2016-08-29 09:18:39 -07002770 this->setupGeometry(primProc, *nonInstMesh, &indexOffsetInBytes);
2771 if (nonInstMesh->isIndexed()) {
egdaniel0e1853c2016-03-17 11:35:45 -07002772 GrGLvoid* indices =
bsalomonfc9527a2016-08-29 09:18:39 -07002773 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes +
2774 sizeof(uint16_t) * nonInstMesh->startIndex());
egdaniel0e1853c2016-03-17 11:35:45 -07002775 // info.startVertex() was accounted for by setupGeometry.
bsalomonfc9527a2016-08-29 09:18:39 -07002776 if (this->glCaps().drawRangeElementsSupport()) {
2777 // We assume here that the batch that generated the mesh used the full
2778 // 0..vertexCount()-1 range.
2779 int start = 0;
2780 int end = nonInstMesh->vertexCount() - 1;
2781 GL_CALL(DrawRangeElements(gPrimitiveType2GLMode[nonInstMesh->primitiveType()],
2782 start, end,
2783 nonInstMesh->indexCount(),
2784 GR_GL_UNSIGNED_SHORT,
2785 indices));
2786 } else {
2787 GL_CALL(DrawElements(gPrimitiveType2GLMode[nonInstMesh->primitiveType()],
2788 nonInstMesh->indexCount(),
2789 GR_GL_UNSIGNED_SHORT,
2790 indices));
2791 }
egdaniel0e1853c2016-03-17 11:35:45 -07002792 } else {
2793 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account
2794 // for startVertex in the DrawElements case. So we always rely on setupGeometry to
2795 // have accounted for startVertex.
bsalomonfc9527a2016-08-29 09:18:39 -07002796 GL_CALL(DrawArrays(gPrimitiveType2GLMode[nonInstMesh->primitiveType()], 0,
2797 nonInstMesh->vertexCount()));
egdaniel0e1853c2016-03-17 11:35:45 -07002798 }
2799 fStats.incNumDraws();
bsalomonfc9527a2016-08-29 09:18:39 -07002800 } while ((nonInstMesh = iter.next()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002801 }
ethannicholas22793252016-01-30 09:59:10 -08002802
2803 if (fHWPLSEnabled && plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2804 // PLS draws always involve multiple draws, finishing up with a non-PLS
2805 // draw that writes to the color buffer. That draw ends up here; we wait
2806 // until after it is complete to actually disable PLS.
2807 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2808 fHWPLSEnabled = false;
2809 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002810 this->disableWindowRectangles();
ethannicholas22793252016-01-30 09:59:10 -08002811 }
2812
bsalomon@google.comd302f142011-03-03 13:54:13 +00002813#if SWAP_PER_DRAW
2814 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002815 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002816 aglSwapBuffers(aglGetCurrentContext());
2817 int set_a_break_pt_here = 9;
2818 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002819 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002820 SwapBuf();
2821 int set_a_break_pt_here = 9;
2822 SwapBuf();
2823 #endif
2824#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002825}
2826
cdaltone2e71c22016-04-07 18:13:29 -07002827void GrGLGpu::stampPLSSetupRect(const SkRect& bounds) {
caryclarkd6562002016-07-27 12:02:07 -07002828 SkASSERT(this->glCaps().glslCaps()->plsPathRenderingSupport());
ethannicholas22793252016-01-30 09:59:10 -08002829
cdaltone2e71c22016-04-07 18:13:29 -07002830 if (!fPLSSetupProgram.fProgram) {
2831 if (!this->createPLSSetupProgram()) {
2832 SkDebugf("Failed to create PLS setup program.\n");
2833 return;
2834 }
2835 }
2836
2837 GL_CALL(UseProgram(fPLSSetupProgram.fProgram));
2838 this->fHWVertexArrayState.setVertexArrayID(this, 0);
2839
2840 GrGLAttribArrayState* attribs = this->fHWVertexArrayState.bindInternalVertexArray(this);
2841 attribs->set(this, 0, fPLSSetupProgram.fArrayBuffer, kVec2f_GrVertexAttribType,
2842 2 * sizeof(GrGLfloat), 0);
ethannicholas22793252016-01-30 09:59:10 -08002843 attribs->disableUnusedArrays(this, 0x1);
2844
cdaltone2e71c22016-04-07 18:13:29 -07002845 GL_CALL(Uniform4f(fPLSSetupProgram.fPosXformUniform, bounds.width(), bounds.height(),
2846 bounds.left(), bounds.top()));
ethannicholas22793252016-01-30 09:59:10 -08002847
2848 GrXferProcessor::BlendInfo blendInfo;
2849 blendInfo.reset();
2850 this->flushBlend(blendInfo, GrSwizzle());
2851 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07002852 this->flushDrawFace(GrDrawFace::kBoth);
ethannicholas22793252016-01-30 09:59:10 -08002853 if (!fHWStencilSettings.isDisabled()) {
2854 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2855 }
2856 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
2857 GL_CALL(UseProgram(fHWProgramID));
2858 if (!fHWStencilSettings.isDisabled()) {
2859 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2860 }
2861}
2862
egdaniel0e1853c2016-03-17 11:35:45 -07002863void GrGLGpu::setupPixelLocalStorage(const GrPipeline& pipeline,
2864 const GrPrimitiveProcessor& primProc) {
ethannicholas22793252016-01-30 09:59:10 -08002865 fPLSHasBeenUsed = true;
halcanary9d524f22016-03-29 09:03:52 -07002866 const SkRect& bounds =
egdaniel0e1853c2016-03-17 11:35:45 -07002867 static_cast<const GrPLSGeometryProcessor&>(primProc).getBounds();
ethannicholas22793252016-01-30 09:59:10 -08002868 // setup pixel local storage -- this means capturing and storing the current framebuffer color
2869 // and initializing the winding counts to zero
egdaniel0e1853c2016-03-17 11:35:45 -07002870 GrRenderTarget* rt = pipeline.getRenderTarget();
ethannicholas22793252016-01-30 09:59:10 -08002871 SkScalar width = SkIntToScalar(rt->width());
2872 SkScalar height = SkIntToScalar(rt->height());
2873 // dst rect edges in NDC (-1 to 1)
2874 // having some issues with rounding, just expand the bounds by 1 and trust the scissor to keep
2875 // it contained properly
2876 GrGLfloat dx0 = 2.0f * (bounds.left() - 1) / width - 1.0f;
2877 GrGLfloat dx1 = 2.0f * (bounds.right() + 1) / width - 1.0f;
2878 GrGLfloat dy0 = -2.0f * (bounds.top() - 1) / height + 1.0f;
2879 GrGLfloat dy1 = -2.0f * (bounds.bottom() + 1) / height + 1.0f;
2880 SkRect deviceBounds = SkRect::MakeXYWH(dx0, dy0, dx1 - dx0, dy1 - dy0);
halcanary9d524f22016-03-29 09:03:52 -07002881
ethannicholas22793252016-01-30 09:59:10 -08002882 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
cdaltone2e71c22016-04-07 18:13:29 -07002883 this->stampPLSSetupRect(deviceBounds);
ethannicholas22793252016-01-30 09:59:10 -08002884}
2885
bsalomon861e1032014-12-16 07:33:49 -08002886void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002887 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002888 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002889 // Some extensions automatically resolves the texture when it is read.
2890 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002891 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2892 fStats.incRenderTargetBinds();
2893 fStats.incRenderTargetBinds();
2894 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2895 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2896 // make sure we go through flushRenderTarget() since we've modified
2897 // the bound DRAW FBO ID.
2898 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002899 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002900 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002901
bsalomon@google.com347c3822013-05-01 20:10:01 +00002902 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002903 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002904 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002905 scissorState.set(dirtyRect);
2906 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002907 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002908 GL_CALL(ResolveMultisampleFramebuffer());
2909 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002910 GrGLIRect r;
2911 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2912 dirtyRect.width(), dirtyRect.height(), target->origin());
2913
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002914 int right = r.fLeft + r.fWidth;
2915 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002916
2917 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002918 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002919 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002920 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2921 r.fLeft, r.fBottom, right, top,
2922 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002923 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002924 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002925 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002926 }
2927}
2928
bsalomon@google.com411dad02012-06-05 20:24:20 +00002929namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002930
bsalomon@google.com411dad02012-06-05 20:24:20 +00002931
2932GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002933 static const GrGLenum gTable[kGrStencilOpCount] = {
2934 GR_GL_KEEP, // kKeep
2935 GR_GL_ZERO, // kZero
2936 GR_GL_REPLACE, // kReplace
2937 GR_GL_INVERT, // kInvert
2938 GR_GL_INCR_WRAP, // kIncWrap
2939 GR_GL_DECR_WRAP, // kDecWrap
2940 GR_GL_INCR, // kIncClamp
2941 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002942 };
cdalton93a379b2016-05-11 13:58:08 -07002943 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2944 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2945 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2946 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2947 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2948 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2949 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2950 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2951 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2952 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002953}
2954
2955void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002956 const GrStencilSettings::Face& face,
2957 GrGLenum glFace) {
2958 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2959 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2960 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002961
cdalton93a379b2016-05-11 13:58:08 -07002962 GrGLint ref = face.fRef;
2963 GrGLint mask = face.fTestMask;
2964 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002965
2966 if (GR_GL_FRONT_AND_BACK == glFace) {
2967 // we call the combined func just in case separate stencil is not
2968 // supported.
2969 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2970 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002971 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002972 } else {
2973 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2974 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002975 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002976 }
2977}
2978}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002979
bsalomon3e791242014-12-17 13:43:13 -08002980void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2981 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002982 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002983 if (kNo_TriState != fHWStencilTestEnabled) {
2984 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2985 fHWStencilTestEnabled = kNo_TriState;
2986 }
2987 } else {
2988 if (kYes_TriState != fHWStencilTestEnabled) {
2989 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2990 fHWStencilTestEnabled = kYes_TriState;
2991 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002992 }
joshualitta58fe352014-10-27 08:39:00 -07002993 if (!stencilSettings.isDisabled()) {
cdalton93a379b2016-05-11 13:58:08 -07002994 if (stencilSettings.isTwoSided()) {
2995 SkASSERT(this->caps()->twoSidedStencilSupport());
bsalomon@google.com411dad02012-06-05 20:24:20 +00002996 set_gl_stencil(this->glInterface(),
cdalton93a379b2016-05-11 13:58:08 -07002997 stencilSettings.front(),
2998 GR_GL_FRONT);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002999 set_gl_stencil(this->glInterface(),
cdalton93a379b2016-05-11 13:58:08 -07003000 stencilSettings.back(),
3001 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00003002 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00003003 set_gl_stencil(this->glInterface(),
cdalton93a379b2016-05-11 13:58:08 -07003004 stencilSettings.front(),
3005 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00003006 }
3007 }
joshualitta58fe352014-10-27 08:39:00 -07003008 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00003009 }
3010}
3011
cdaltonaf8bc7d2016-02-05 09:35:20 -08003012void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08003013 // rt is only optional if useHWAA is false.
3014 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07003015 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00003016
csmartdalton2b5f2cb2016-06-10 14:06:32 -07003017 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07003018 if (useHWAA) {
3019 if (kYes_TriState != fMSAAEnabled) {
3020 GL_CALL(Enable(GR_GL_MULTISAMPLE));
3021 fMSAAEnabled = kYes_TriState;
3022 }
3023 } else {
3024 if (kNo_TriState != fMSAAEnabled) {
3025 GL_CALL(Disable(GR_GL_MULTISAMPLE));
3026 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003027 }
3028 }
3029 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08003030
3031 if (0 != this->caps()->maxRasterSamples()) {
csmartdaltonf9635992016-08-10 11:09:07 -07003032 if (useHWAA && rt->isMixedSampled() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08003033 // Since stencil is disabled and we want more samples than are in the color buffer, we
3034 // need to tell the rasterizer explicitly how many to run.
3035 if (kYes_TriState != fHWRasterMultisampleEnabled) {
3036 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
3037 fHWRasterMultisampleEnabled = kYes_TriState;
3038 }
3039 if (rt->numStencilSamples() != fHWNumRasterSamples) {
3040 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
3041 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
3042 fHWNumRasterSamples = rt->numStencilSamples();
3043 }
3044 } else {
3045 if (kNo_TriState != fHWRasterMultisampleEnabled) {
3046 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
3047 fHWRasterMultisampleEnabled = kNo_TriState;
3048 }
3049 }
3050 } else {
csmartdaltonf9635992016-08-10 11:09:07 -07003051 SkASSERT(!useHWAA || !rt->isMixedSampled() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003052 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003053}
3054
bsalomon7f9b2e42016-01-12 13:29:26 -08003055void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07003056 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07003057 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07003058
cdalton8917d622015-05-06 13:40:21 -07003059 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08003060 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
3061 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07003062 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
3063 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07003064 if (blendOff) {
3065 if (kNo_TriState != fHWBlendState.fEnabled) {
3066 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07003067
3068 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
3069 // https://code.google.com/p/skia/issues/detail?id=3943
3070 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
3071 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
3072 SkASSERT(this->caps()->advancedBlendEquationSupport());
3073 // Set to any basic blending equation.
3074 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
3075 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
3076 fHWBlendState.fEquation = blend_equation;
3077 }
3078
egdanielb414f252014-07-29 13:15:47 -07003079 fHWBlendState.fEnabled = kNo_TriState;
3080 }
cdalton8917d622015-05-06 13:40:21 -07003081 return;
3082 }
3083
3084 if (kYes_TriState != fHWBlendState.fEnabled) {
3085 GL_CALL(Enable(GR_GL_BLEND));
3086 fHWBlendState.fEnabled = kYes_TriState;
3087 }
3088
3089 if (fHWBlendState.fEquation != equation) {
3090 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
3091 fHWBlendState.fEquation = equation;
3092 }
3093
3094 if (GrBlendEquationIsAdvanced(equation)) {
3095 SkASSERT(this->caps()->advancedBlendEquationSupport());
3096 // Advanced equations have no other blend state.
3097 return;
3098 }
3099
bsalomone63ffef2016-02-05 07:17:34 -08003100 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07003101 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
3102 gXfermodeCoeff2Blend[dstCoeff]));
3103 fHWBlendState.fSrcCoeff = srcCoeff;
3104 fHWBlendState.fDstCoeff = dstCoeff;
3105 }
3106
bsalomon7f9b2e42016-01-12 13:29:26 -08003107 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
3108 GrColor blendConst = blendInfo.fBlendConstant;
3109 blendConst = swizzle.applyTo(blendConst);
3110 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
3111 GrGLfloat c[4];
3112 GrColorToRGBAFloat(blendConst, c);
3113 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
3114 fHWBlendState.fConstColor = blendConst;
3115 fHWBlendState.fConstColorValid = true;
3116 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00003117 }
3118}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00003119
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003120static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00003121 static const GrGLenum gWrapModes[] = {
3122 GR_GL_CLAMP_TO_EDGE,
3123 GR_GL_REPEAT,
3124 GR_GL_MIRRORED_REPEAT
3125 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00003126 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00003127 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
3128 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
3129 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
3130 return gWrapModes[tm];
3131}
3132
egdanielb7e7d572015-11-04 04:23:53 -08003133static GrGLenum get_component_enum_from_char(char component) {
3134 switch (component) {
3135 case 'r':
3136 return GR_GL_RED;
3137 case 'g':
3138 return GR_GL_GREEN;
3139 case 'b':
3140 return GR_GL_BLUE;
3141 case 'a':
3142 return GR_GL_ALPHA;
3143 default:
3144 SkFAIL("Unsupported component");
3145 return 0;
3146 }
3147}
3148
3149/** If texture swizzling is available using tex parameters then it is preferred over mangling
3150 the generated shader code. This potentially allows greater reuse of cached shaders. */
3151static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08003152 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08003153 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08003154 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08003155 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08003156 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08003157 }
3158}
3159
brianosmana167e742016-05-24 06:18:48 -07003160void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -07003161 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07003162 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003163
reed856e9d92015-09-30 12:21:45 -07003164#ifdef SK_DEBUG
3165 if (!this->caps()->npotTextureTileSupport()) {
3166 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
3167 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
3168 if (tileX || tileY) {
3169 const int w = texture->width();
3170 const int h = texture->height();
3171 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3172 }
3173 }
3174#endif
3175
bsalomon@google.comb8670992012-07-25 21:27:09 +00003176 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3177 // 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 +00003178 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003179 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003180 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003181 this->onResolveRenderTarget(texRT);
3182 }
3183
robertphillips8abb3702016-08-31 14:04:06 -07003184 uint32_t textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003185 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003186 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003187 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003188 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003189 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003190 }
3191
bsalomon@google.com4c883782012-06-04 19:05:11 +00003192 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003193 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003194 bool setAll = timestamp < this->getResetTimestamp();
3195 GrGLTexture::TexParams newTexParams;
3196
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003197 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003198 GR_GL_NEAREST,
3199 GR_GL_LINEAR,
3200 GR_GL_LINEAR_MIPMAP_LINEAR
3201 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003202 static GrGLenum glMagFilterModes[] = {
3203 GR_GL_NEAREST,
3204 GR_GL_LINEAR,
3205 GR_GL_LINEAR
3206 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003207 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003208
3209 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3210 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3211 filterMode = GrTextureParams::kBilerp_FilterMode;
3212 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003213 }
bsalomonefd7d452014-10-23 14:17:46 -07003214
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003215 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3216 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003217
brianosman33f6b3f2016-06-02 05:49:21 -07003218 if (GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003219 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003220 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003221 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003222 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003223 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003224 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003225
brianosman33f6b3f2016-06-02 05:49:21 -07003226#ifdef SK_DEBUG
3227 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
3228 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3229 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3230 if (GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003231 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs ?
3232 SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
3233 SkASSERT(texture->texturePriv().gammaTreatment() == gammaTreatment);
brianosman33f6b3f2016-06-02 05:49:21 -07003234 }
3235 }
3236#endif
3237
cblume55f2d2d2016-02-26 13:20:48 -08003238 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3239
bsalomon@google.comb8670992012-07-25 21:27:09 +00003240 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3241 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003242 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003243 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003244 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003245 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003246 }
3247 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3248 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003249 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003250 }
cblume55f2d2d2016-02-26 13:20:48 -08003251 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003252 // These are not supported in ES2 contexts
3253 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3254 if (newTexParams.fMaxMipMapLevel != 0) {
3255 this->setTextureUnit(unitIdx);
3256 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3257 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3258 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3259 newTexParams.fMaxMipMapLevel));
3260 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3261 newTexParams.fMaxMipMapLevel));
3262 }
cblume55f2d2d2016-02-26 13:20:48 -08003263 }
3264 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003265 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003266 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003267 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003268 }
3269 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003270 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003271 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003272 }
bsalomoncdee0092016-01-08 13:20:12 -08003273 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003274 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3275 oldTexParams.fSwizzleRGBA,
3276 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003277 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003278 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003279 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003280}
3281
csmartdalton1897cfd2016-06-03 08:50:54 -07003282void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003283 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3284 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003285
3286 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3287
3288 if (!buffTex.fKnownBound) {
3289 if (!buffTex.fTextureID) {
3290 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3291 if (!buffTex.fTextureID) {
3292 return;
3293 }
3294 }
3295
3296 this->setTextureUnit(unitIdx);
3297 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3298
3299 buffTex.fKnownBound = true;
3300 }
3301
robertphillips8abb3702016-08-31 14:04:06 -07003302 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003303 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003304
3305 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003306 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3307 this->glCaps().configSizedInternalFormat(texelConfig),
3308 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003309
cdalton74b8d322016-04-11 14:47:28 -07003310 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003311 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003312
3313 if (this->glCaps().textureSwizzleSupport() &&
3314 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3315 GrGLenum glSwizzle[4];
3316 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3317 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3318 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3319 }
3320
3321 buffer->setHasAttachedToTexture();
3322 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3323 }
3324}
3325
brianosman33f6b3f2016-06-02 05:49:21 -07003326void GrGLGpu::generateMipmaps(const GrTextureParams& params, bool allowSRGBInputs,
3327 GrGLTexture* texture) {
3328 SkASSERT(texture);
3329
3330 // First, figure out if we need mips for this texture at all:
3331 GrTextureParams::FilterMode filterMode = params.filterMode();
3332
3333 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3334 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3335 filterMode = GrTextureParams::kBilerp_FilterMode;
3336 }
3337 }
3338
3339 if (GrTextureParams::kMipMap_FilterMode != filterMode) {
3340 return;
3341 }
3342
3343 // If this is an sRGB texture and the mips were previously built the "other" way
3344 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3345 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
brianosmanfe199872016-06-13 07:59:48 -07003346 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs
3347 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
brianosman33f6b3f2016-06-02 05:49:21 -07003348 if (GrPixelConfigIsSRGB(texture->config()) &&
brianosmanfe199872016-06-13 07:59:48 -07003349 gammaTreatment != texture->texturePriv().gammaTreatment()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003350 texture->texturePriv().dirtyMipMaps(true);
3351 }
3352
3353 // If the mips aren't dirty, we're done:
3354 if (!texture->texturePriv().mipMapsAreDirty()) {
3355 return;
3356 }
3357
3358 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3359 // from the rt it will still be the last bound texture, but it needs resolving.
3360 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3361 if (texRT) {
3362 this->onResolveRenderTarget(texRT);
3363 }
3364
3365 GrGLenum target = texture->target();
3366 this->setScratchTextureUnit();
3367 GL_CALL(BindTexture(target, texture->textureID()));
3368
3369 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3370 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
3371 if (GrPixelConfigIsSRGB(texture->config())) {
3372 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT,
3373 allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT));
3374 }
3375
3376 // Either do manual mipmap generation or (if that fails), just rely on the driver:
3377 if (!this->generateMipmap(texture, allowSRGBInputs)) {
3378 GL_CALL(GenerateMipmap(target));
3379 }
3380
brianosmanfe199872016-06-13 07:59:48 -07003381 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003382 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3383 texture->width(), texture->height()));
brianosmanfe199872016-06-13 07:59:48 -07003384 texture->texturePriv().setGammaTreatment(gammaTreatment);
brianosman33f6b3f2016-06-02 05:49:21 -07003385
3386 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3387 texture->textureParamsModified();
3388}
3389
cdalton74b8d322016-04-11 14:47:28 -07003390void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3391 this->setTextureUnit(unitIdx);
3392 if (this->glStandard() == kGLES_GrGLStandard) {
3393 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3394 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3395 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3396 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3397 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3398 } else {
3399 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3400 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3401 reinterpret_cast<const GrGLint*>(swizzle)));
3402 }
3403}
3404
egdaniel080e6732014-12-22 07:35:52 -08003405void GrGLGpu::flushColorWrite(bool writeColor) {
3406 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003407 if (kNo_TriState != fHWWriteToColor) {
3408 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3409 GR_GL_FALSE, GR_GL_FALSE));
3410 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003411 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003412 } else {
3413 if (kYes_TriState != fHWWriteToColor) {
3414 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3415 fHWWriteToColor = kYes_TriState;
3416 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003417 }
bsalomon3e791242014-12-17 13:43:13 -08003418}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003419
robertphillips5fa7f302016-07-21 09:21:04 -07003420void GrGLGpu::flushDrawFace(GrDrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003421 if (fHWDrawFace != face) {
3422 switch (face) {
robertphillips5fa7f302016-07-21 09:21:04 -07003423 case GrDrawFace::kCCW:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003424 GL_CALL(Enable(GR_GL_CULL_FACE));
3425 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003426 break;
robertphillips5fa7f302016-07-21 09:21:04 -07003427 case GrDrawFace::kCW:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003428 GL_CALL(Enable(GR_GL_CULL_FACE));
3429 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003430 break;
robertphillips5fa7f302016-07-21 09:21:04 -07003431 case GrDrawFace::kBoth:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003432 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003433 break;
3434 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003435 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003436 }
bsalomon3e791242014-12-17 13:43:13 -08003437 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003438 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003439}
3440
bsalomon861e1032014-12-16 07:33:49 -08003441void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003442 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003443 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003444 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003445 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003446 }
3447}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003448
bsalomon861e1032014-12-16 07:33:49 -08003449void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003450 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003451 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003452 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3453 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3454 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003455 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003456 // clear out the this field so that if a program does use this unit it will rebind the correct
3457 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07003458 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003459}
3460
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003461// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon7ea33f52015-11-22 14:51:00 -08003462static inline bool can_blit_framebuffer(const GrSurface* dst,
3463 const GrSurface* src,
3464 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003465 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
bsalomon083617b2016-02-12 12:10:14 -08003466 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0)) {
3467 switch (gpu->glCaps().blitFramebufferSupport()) {
3468 case GrGLCaps::kNone_BlitFramebufferSupport:
3469 return false;
3470 case GrGLCaps::kNoScalingNoMirroring_BlitFramebufferSupport:
3471 // Our copy surface doesn't support scaling so just check for mirroring.
3472 if (dst->origin() != src->origin()) {
3473 return false;
3474 }
3475 break;
3476 case GrGLCaps::kFull_BlitFramebufferSupport:
3477 break;
3478 }
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00003479 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
3480 // or the rects are not the same (not just the same size but have the same edges).
3481 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
3482 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
3483 return false;
3484 }
bsalomon7ea33f52015-11-22 14:51:00 -08003485 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3486 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3487 return false;
3488 }
3489 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3490 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3491 return false;
3492 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003493 return true;
3494 } else {
3495 return false;
3496 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003497}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003498
bsalomon7ea33f52015-11-22 14:51:00 -08003499static inline bool can_copy_texsubimage(const GrSurface* dst,
3500 const GrSurface* src,
3501 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003502 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3503 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3504 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003505 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003506 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3507 return false;
3508 }
3509 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3510 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3511 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003512 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003513 return false;
3514 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003515 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3516 // If the src is multisampled (and uses an extension where there is a separate MSAA
3517 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003518 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003519 return false;
3520 }
bsalomon7ea33f52015-11-22 14:51:00 -08003521
3522 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3523 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3524 // texture.
3525 if (!dstTex) {
3526 return false;
3527 }
3528
3529 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003530
bsalomon7ea33f52015-11-22 14:51:00 -08003531 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3532 // is required.
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003533 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003534 !GrPixelConfigIsCompressed(src->config()) &&
3535 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) &&
3536 dstTex->target() == GR_GL_TEXTURE_2D &&
3537 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003538 return true;
3539 } else {
3540 return false;
3541 }
3542}
3543
3544// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3545// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07003546void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3547 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003548 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003549 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003550 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003551 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003552 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003553 GrGLuint* tempFBOID;
3554 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003555
egdanield803f272015-03-18 13:01:52 -07003556 if (0 == *tempFBOID) {
3557 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003558 }
3559
egdanield803f272015-03-18 13:01:52 -07003560 fStats.incRenderTargetBinds();
3561 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3562 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003563 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003564 target,
robertphillips754f4e92014-09-18 13:52:08 -07003565 texID,
3566 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003567 viewport->fLeft = 0;
3568 viewport->fBottom = 0;
3569 viewport->fWidth = surface->width();
3570 viewport->fHeight = surface->height();
3571 } else {
egdanield803f272015-03-18 13:01:52 -07003572 fStats.incRenderTargetBinds();
3573 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003574 *viewport = rt->getViewport();
3575 }
egdaniel0f5f9672015-02-03 11:10:51 -08003576}
3577
bsalomon10528f12015-10-14 12:54:52 -07003578void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
cblume61214052016-01-26 09:10:48 -08003579 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003580 if (!surface->asRenderTarget()) {
3581 SkASSERT(surface->asTexture());
3582 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3583 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3584 GR_GL_COLOR_ATTACHMENT0,
3585 textureTarget,
3586 0,
3587 0));
3588 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003589}
3590
egdaniel4bcd62e2016-08-31 07:37:31 -07003591bool GrGLGpu::initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07003592 // If the src is a texture, we can implement the blit as a draw assuming the config is
3593 // renderable.
3594 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
3595 desc->fOrigin = kDefault_GrSurfaceOrigin;
3596 desc->fFlags = kRenderTarget_GrSurfaceFlag;
3597 desc->fConfig = src->config();
3598 return true;
3599 }
3600
bsalomon7ea33f52015-11-22 14:51:00 -08003601 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
3602 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
3603 // Not supported for FBO blit or CopyTexSubImage
3604 return false;
3605 }
3606
bsalomon6df86402015-06-01 10:41:49 -07003607 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08003608 // possible and we return false to fallback to creating a render target dst for render-to-
3609 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
3610 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
3611
bsalomon5c7d62d2016-06-08 10:02:42 -07003612 GrSurfaceOrigin originForBlitFramebuffer = kDefault_GrSurfaceOrigin;
3613 if (this->glCaps().blitFramebufferSupport() ==
brianosman1d202582016-06-22 08:30:40 -07003614 GrGLCaps::kNoScalingNoMirroring_BlitFramebufferSupport) {
bsalomon5c7d62d2016-06-08 10:02:42 -07003615 originForBlitFramebuffer = src->origin();
3616 }
3617
bsalomon@google.comeb851172013-04-15 13:51:00 +00003618 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003619 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003620 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003621 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
3622 // then we set up for that, otherwise fail.
3623 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
bsalomon5c7d62d2016-06-08 10:02:42 -07003624 desc->fOrigin = originForBlitFramebuffer;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003625 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003626 desc->fConfig = kBGRA_8888_GrPixelConfig;
3627 return true;
3628 }
3629 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00003630 }
3631
egdaniel4bcd62e2016-08-31 07:37:31 -07003632 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src);
3633 if (srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003634 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
3635 // fail.
3636 if (this->caps()->isConfigRenderable(src->config(), false)) {
bsalomon5c7d62d2016-06-08 10:02:42 -07003637 desc->fOrigin = originForBlitFramebuffer;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003638 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003639 desc->fConfig = src->config();
3640 return true;
3641 }
3642 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00003643 }
bsalomonf90a02b2014-11-26 12:28:00 -08003644
3645 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
3646 desc->fConfig = src->config();
3647 desc->fOrigin = src->origin();
3648 desc->fFlags = kNone_GrSurfaceFlags;
3649 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003650}
3651
joshualitt1cbdcde2015-08-21 11:53:29 -07003652bool GrGLGpu::onCopySurface(GrSurface* dst,
3653 GrSurface* src,
3654 const SkIRect& srcRect,
3655 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003656 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3657 // swizzle.
3658 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
3659 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
3660 return false;
3661 }
bsalomon083617b2016-02-12 12:10:14 -08003662 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3663 bool preferCopy = SkToBool(dst->asRenderTarget());
3664 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003665 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3666 return true;
3667 }
bsalomon5df6fee2015-05-18 06:26:15 -07003668 }
cblume61214052016-01-26 09:10:48 -08003669
bsalomon6df86402015-06-01 10:41:49 -07003670 if (can_copy_texsubimage(dst, src, this)) {
3671 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3672 return true;
3673 }
3674
mtklein404b3b22015-05-18 09:29:10 -07003675 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003676 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3677 }
3678
bsalomon083617b2016-02-12 12:10:14 -08003679 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003680 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3681 return true;
3682 }
bsalomon083617b2016-02-12 12:10:14 -08003683 }
3684
bsalomon6df86402015-06-01 10:41:49 -07003685 return false;
3686}
3687
cdaltone2e71c22016-04-07 18:13:29 -07003688bool GrGLGpu::createCopyProgram(int progIdx) {
cdaltonc08f1962016-02-12 12:14:06 -08003689 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
egdaniel990dbc82016-07-13 14:09:30 -07003690 static const GrSLType kSamplerTypes[3] = { kTexture2DSampler_GrSLType,
3691 kTextureExternalSampler_GrSLType,
3692 kTexture2DRectSampler_GrSLType };
3693 if (kTextureExternalSampler_GrSLType == kSamplerTypes[progIdx] &&
cdaltone2e71c22016-04-07 18:13:29 -07003694 !this->glCaps().glslCaps()->externalTextureSupport()) {
3695 return false;
cdaltondeacc972016-04-06 14:26:33 -07003696 }
egdaniel990dbc82016-07-13 14:09:30 -07003697 if (kTexture2DRectSampler_GrSLType == kSamplerTypes[progIdx] &&
cdaltone2e71c22016-04-07 18:13:29 -07003698 !this->glCaps().rectangleTextureSupport()) {
3699 return false;
3700 }
3701
3702 if (!fCopyProgramArrayBuffer) {
3703 static const GrGLfloat vdata[] = {
3704 0, 0,
3705 0, 1,
3706 1, 0,
3707 1, 1
3708 };
3709 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3710 kStatic_GrAccessPattern, vdata));
3711 }
3712 if (!fCopyProgramArrayBuffer) {
3713 return false;
3714 }
3715
3716 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3717 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3718 if (!fCopyPrograms[progIdx].fProgram) {
3719 return false;
3720 }
3721
3722 const char* version = glslCaps->versionDeclString();
3723 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3724 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3725 GrShaderVar::kUniform_TypeModifier);
3726 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
3727 GrShaderVar::kUniform_TypeModifier);
3728 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[progIdx],
3729 GrShaderVar::kUniform_TypeModifier);
3730 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
3731 GrShaderVar::kVaryingOut_TypeModifier);
3732 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3733 GrShaderVar::kOut_TypeModifier);
3734
3735 SkString vshaderTxt(version);
3736 if (glslCaps->noperspectiveInterpolationSupport()) {
3737 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3738 vshaderTxt.appendf("#extension %s : require\n", extension);
3739 }
3740 vTexCoord.addModifier("noperspective");
3741 }
3742
3743 aVertex.appendDecl(glslCaps, &vshaderTxt);
3744 vshaderTxt.append(";");
3745 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
3746 vshaderTxt.append(";");
3747 uPosXform.appendDecl(glslCaps, &vshaderTxt);
3748 vshaderTxt.append(";");
3749 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
3750 vshaderTxt.append(";");
3751
3752 vshaderTxt.append(
3753 "// Copy Program VS\n"
3754 "void main() {"
3755 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3756 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3757 " gl_Position.zw = vec2(0, 1);"
3758 "}"
3759 );
3760
3761 SkString fshaderTxt(version);
3762 if (glslCaps->noperspectiveInterpolationSupport()) {
3763 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3764 fshaderTxt.appendf("#extension %s : require\n", extension);
3765 }
3766 }
egdaniel990dbc82016-07-13 14:09:30 -07003767 if (kSamplerTypes[progIdx] == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003768 fshaderTxt.appendf("#extension %s : require\n",
3769 glslCaps->externalTextureExtensionString());
3770 }
3771 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
3772 &fshaderTxt);
3773 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3774 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
3775 fshaderTxt.append(";");
3776 uTexture.appendDecl(glslCaps, &fshaderTxt);
3777 fshaderTxt.append(";");
3778 const char* fsOutName;
3779 if (glslCaps->mustDeclareFragmentShaderOutput()) {
3780 oFragColor.appendDecl(glslCaps, &fshaderTxt);
3781 fshaderTxt.append(";");
3782 fsOutName = oFragColor.c_str();
3783 } else {
3784 fsOutName = "gl_FragColor";
3785 }
3786 fshaderTxt.appendf(
3787 "// Copy Program FS\n"
3788 "void main() {"
3789 " %s = %s(u_texture, v_texCoord);"
3790 "}",
3791 fsOutName,
3792 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[progIdx], this->glslGeneration())
3793 );
3794
3795 const char* str;
3796 GrGLint length;
3797
3798 str = vshaderTxt.c_str();
3799 length = SkToInt(vshaderTxt.size());
3800 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3801 GR_GL_VERTEX_SHADER, &str, &length, 1,
3802 &fStats);
3803
3804 str = fshaderTxt.c_str();
3805 length = SkToInt(fshaderTxt.size());
3806 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3807 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3808 &fStats);
3809
3810 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3811
3812 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3813 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3814 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3815 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3816 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3817 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3818
3819 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3820
3821 GL_CALL(DeleteShader(vshader));
3822 GL_CALL(DeleteShader(fshader));
3823
3824 return true;
bsalomon6df86402015-06-01 10:41:49 -07003825}
3826
brianosman33f6b3f2016-06-02 05:49:21 -07003827bool GrGLGpu::createMipmapProgram(int progIdx) {
3828 const bool oddWidth = SkToBool(progIdx & 0x2);
3829 const bool oddHeight = SkToBool(progIdx & 0x1);
3830 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3831
3832 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
3833
3834 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3835 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3836 if (!fMipmapPrograms[progIdx].fProgram) {
3837 return false;
3838 }
3839
3840 const char* version = glslCaps->versionDeclString();
3841 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3842 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3843 GrShaderVar::kUniform_TypeModifier);
egdaniel990dbc82016-07-13 14:09:30 -07003844 GrGLSLShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3845 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003846 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
3847 GrGLSLShaderVar vTexCoords[] = {
3848 GrGLSLShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3849 GrGLSLShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3850 GrGLSLShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3851 GrGLSLShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier),
3852 };
3853 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3854 GrShaderVar::kOut_TypeModifier);
3855
3856 SkString vshaderTxt(version);
3857 if (glslCaps->noperspectiveInterpolationSupport()) {
3858 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3859 vshaderTxt.appendf("#extension %s : require\n", extension);
3860 }
3861 vTexCoords[0].addModifier("noperspective");
3862 vTexCoords[1].addModifier("noperspective");
3863 vTexCoords[2].addModifier("noperspective");
3864 vTexCoords[3].addModifier("noperspective");
3865 }
3866
3867 aVertex.appendDecl(glslCaps, &vshaderTxt);
3868 vshaderTxt.append(";");
3869 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
3870 vshaderTxt.append(";");
3871 for (int i = 0; i < numTaps; ++i) {
3872 vTexCoords[i].appendDecl(glslCaps, &vshaderTxt);
3873 vshaderTxt.append(";");
3874 }
3875
3876 vshaderTxt.append(
3877 "// Mipmap Program VS\n"
3878 "void main() {"
3879 " gl_Position.xy = a_vertex * vec2(2, 2) - vec2(1, 1);"
3880 " gl_Position.zw = vec2(0, 1);"
3881 );
3882
3883 // Insert texture coordinate computation:
3884 if (oddWidth && oddHeight) {
3885 vshaderTxt.append(
3886 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
3887 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + vec2(u_texCoordXform.x, 0);"
3888 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + vec2(0, u_texCoordXform.z);"
3889 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3890 );
3891 } else if (oddWidth) {
3892 vshaderTxt.append(
3893 " v_texCoord0 = a_vertex.xy * vec2(u_texCoordXform.y, 1);"
3894 " v_texCoord1 = a_vertex.xy * vec2(u_texCoordXform.y, 1) + vec2(u_texCoordXform.x, 0);"
3895 );
3896 } else if (oddHeight) {
3897 vshaderTxt.append(
3898 " v_texCoord0 = a_vertex.xy * vec2(1, u_texCoordXform.w);"
3899 " v_texCoord1 = a_vertex.xy * vec2(1, u_texCoordXform.w) + vec2(0, u_texCoordXform.z);"
3900 );
3901 } else {
3902 vshaderTxt.append(
3903 " v_texCoord0 = a_vertex.xy;"
3904 );
3905 }
3906
3907 vshaderTxt.append("}");
3908
3909 SkString fshaderTxt(version);
3910 if (glslCaps->noperspectiveInterpolationSupport()) {
3911 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3912 fshaderTxt.appendf("#extension %s : require\n", extension);
3913 }
3914 }
3915 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
3916 &fshaderTxt);
3917 for (int i = 0; i < numTaps; ++i) {
3918 vTexCoords[i].setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3919 vTexCoords[i].appendDecl(glslCaps, &fshaderTxt);
3920 fshaderTxt.append(";");
3921 }
3922 uTexture.appendDecl(glslCaps, &fshaderTxt);
3923 fshaderTxt.append(";");
3924 const char* fsOutName;
3925 if (glslCaps->mustDeclareFragmentShaderOutput()) {
3926 oFragColor.appendDecl(glslCaps, &fshaderTxt);
3927 fshaderTxt.append(";");
3928 fsOutName = oFragColor.c_str();
3929 } else {
3930 fsOutName = "gl_FragColor";
3931 }
egdaniel990dbc82016-07-13 14:09:30 -07003932 const char* sampleFunction = GrGLSLTexture2DFunctionName(kVec2f_GrSLType,
3933 kTexture2DSampler_GrSLType,
brianosman33f6b3f2016-06-02 05:49:21 -07003934 this->glslGeneration());
3935 fshaderTxt.append(
3936 "// Mipmap Program FS\n"
3937 "void main() {"
3938 );
3939
3940 if (oddWidth && oddHeight) {
3941 fshaderTxt.appendf(
3942 " %s = (%s(u_texture, v_texCoord0) + %s(u_texture, v_texCoord1) + "
3943 " %s(u_texture, v_texCoord2) + %s(u_texture, v_texCoord3)) * 0.25;",
3944 fsOutName, sampleFunction, sampleFunction, sampleFunction, sampleFunction
3945 );
3946 } else if (oddWidth || oddHeight) {
3947 fshaderTxt.appendf(
3948 " %s = (%s(u_texture, v_texCoord0) + %s(u_texture, v_texCoord1)) * 0.5;",
3949 fsOutName, sampleFunction, sampleFunction
3950 );
3951 } else {
3952 fshaderTxt.appendf(
3953 " %s = %s(u_texture, v_texCoord0);",
3954 fsOutName, sampleFunction
3955 );
3956 }
3957
3958 fshaderTxt.append("}");
3959
3960 const char* str;
3961 GrGLint length;
3962
3963 str = vshaderTxt.c_str();
3964 length = SkToInt(vshaderTxt.size());
3965 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3966 GR_GL_VERTEX_SHADER, &str, &length, 1,
3967 &fStats);
3968
3969 str = fshaderTxt.c_str();
3970 length = SkToInt(fshaderTxt.size());
3971 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3972 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3973 &fStats);
3974
3975 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3976
3977 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3978 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3979 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3980 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3981
3982 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3983
3984 GL_CALL(DeleteShader(vshader));
3985 GL_CALL(DeleteShader(fshader));
3986
3987 return true;
3988}
3989
cdaltone2e71c22016-04-07 18:13:29 -07003990bool GrGLGpu::createWireRectProgram() {
3991 if (!fWireRectArrayBuffer) {
3992 static const GrGLfloat vdata[] = {
3993 0, 0,
3994 0, 1,
3995 1, 1,
3996 1, 0
3997 };
3998 fWireRectArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3999 kStatic_GrAccessPattern, vdata));
4000 if (!fWireRectArrayBuffer) {
4001 return false;
4002 }
4003 }
4004
bsalomon6dea83f2015-12-03 12:58:06 -08004005 SkASSERT(!fWireRectProgram.fProgram);
cdaltone2e71c22016-04-07 18:13:29 -07004006 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
4007 if (!fWireRectProgram.fProgram) {
4008 return false;
4009 }
4010
bsalomon6dea83f2015-12-03 12:58:06 -08004011 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
4012 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
4013 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
4014 const char* version = this->glCaps().glslCaps()->versionDeclString();
4015
4016 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
4017 // program is used with a vbo containing the unit square. Vertices are computed from the rect
4018 // uniform using the 4 vbo vertices.
4019 SkString vshaderTxt(version);
4020 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
4021 vshaderTxt.append(";");
4022 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
4023 vshaderTxt.append(";");
4024 vshaderTxt.append(
4025 "// Wire Rect Program VS\n"
4026 "void main() {"
4027 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
4028 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
4029 " gl_Position.zw = vec2(0, 1);"
4030 "}"
4031 );
4032
4033 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
4034
4035 SkString fshaderTxt(version);
4036 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
4037 *this->glCaps().glslCaps(),
4038 &fshaderTxt);
4039 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
4040 fshaderTxt.append(";");
4041 const char* fsOutName;
4042 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
4043 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
4044 fshaderTxt.append(";");
4045 fsOutName = oFragColor.c_str();
4046 } else {
4047 fsOutName = "gl_FragColor";
4048 }
4049 fshaderTxt.appendf(
4050 "// Write Rect Program FS\n"
4051 "void main() {"
4052 " %s = %s;"
4053 "}",
4054 fsOutName,
4055 uColor.c_str()
4056 );
4057
bsalomon6dea83f2015-12-03 12:58:06 -08004058 const char* str;
4059 GrGLint length;
4060
4061 str = vshaderTxt.c_str();
4062 length = SkToInt(vshaderTxt.size());
4063 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
4064 GR_GL_VERTEX_SHADER, &str, &length, 1,
4065 &fStats);
4066
4067 str = fshaderTxt.c_str();
4068 length = SkToInt(fshaderTxt.size());
4069 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
4070 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
4071 &fStats);
4072
4073 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
4074
4075 GL_CALL_RET(fWireRectProgram.fColorUniform,
4076 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
4077 GL_CALL_RET(fWireRectProgram.fRectUniform,
4078 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
4079 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
4080
4081 GL_CALL(DeleteShader(vshader));
4082 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07004083
4084 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08004085}
4086
4087void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08004088 // TODO: This should swizzle the output to match dst's config, though it is a debugging
4089 // visualization.
4090
bsalomon6dea83f2015-12-03 12:58:06 -08004091 this->handleDirtyContext();
4092 if (!fWireRectProgram.fProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07004093 if (!this->createWireRectProgram()) {
4094 SkDebugf("Failed to create wire rect program.\n");
4095 return;
4096 }
bsalomon6dea83f2015-12-03 12:58:06 -08004097 }
4098
4099 int w = rt->width();
4100 int h = rt->height();
4101
4102 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
4103 // whether the render target is flipped or not.
4104 GrGLfloat edges[4];
4105 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
4106 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
4107 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
4108 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
4109 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
4110 } else {
4111 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
4112 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
4113 }
4114 edges[0] = 2 * edges[0] / w - 1.0f;
4115 edges[1] = 2 * edges[1] / h - 1.0f;
4116 edges[2] = 2 * edges[2] / w - 1.0f;
4117 edges[3] = 2 * edges[3] / h - 1.0f;
4118
4119 GrGLfloat channels[4];
4120 static const GrGLfloat scale255 = 1.f / 255.f;
4121 channels[0] = GrColorUnpackR(color) * scale255;
4122 channels[1] = GrColorUnpackG(color) * scale255;
4123 channels[2] = GrColorUnpackB(color) * scale255;
4124 channels[3] = GrColorUnpackA(color) * scale255;
4125
4126 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
4127 this->flushRenderTarget(glRT, &rect);
4128
4129 GL_CALL(UseProgram(fWireRectProgram.fProgram));
4130 fHWProgramID = fWireRectProgram.fProgram;
4131
cdaltone2e71c22016-04-07 18:13:29 -07004132 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08004133
cdaltone2e71c22016-04-07 18:13:29 -07004134 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
cdalton793dc262016-02-08 10:11:47 -08004135 attribs->set(this, 0, fWireRectArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
4136 0);
bsalomon6dea83f2015-12-03 12:58:06 -08004137 attribs->disableUnusedArrays(this, 0x1);
4138
4139 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
4140 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
4141
4142 GrXferProcessor::BlendInfo blendInfo;
4143 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004144 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08004145 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004146 this->flushDrawFace(GrDrawFace::kBoth);
cdaltonaf8bc7d2016-02-05 09:35:20 -08004147 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08004148 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004149 this->disableWindowRectangles();
bsalomon6dea83f2015-12-03 12:58:06 -08004150 GrStencilSettings stencil;
4151 stencil.setDisabled();
4152 this->flushStencil(stencil);
4153
4154 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
4155}
4156
4157
cdaltone2e71c22016-04-07 18:13:29 -07004158bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07004159 GrSurface* src,
4160 const SkIRect& srcRect,
4161 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07004162 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
4163 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
4164
4165 if (!fCopyPrograms[progIdx].fProgram) {
4166 if (!this->createCopyProgram(progIdx)) {
4167 SkDebugf("Failed to create copy program.\n");
4168 return false;
4169 }
4170 }
4171
bsalomon6df86402015-06-01 10:41:49 -07004172 int w = srcRect.width();
4173 int h = srcRect.height();
4174
bsalomon6df86402015-06-01 10:41:49 -07004175 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07004176 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07004177
bsalomon083617b2016-02-12 12:10:14 -08004178 GrGLIRect dstVP;
4179 this->bindSurfaceFBOForCopy(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4180 this->flushViewport(dstVP);
4181 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4182
bsalomon6df86402015-06-01 10:41:49 -07004183 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07004184
bsalomon7ea33f52015-11-22 14:51:00 -08004185 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
4186 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07004187
cdaltone2e71c22016-04-07 18:13:29 -07004188 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07004189
cdaltone2e71c22016-04-07 18:13:29 -07004190 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
cdalton793dc262016-02-08 10:11:47 -08004191 attribs->set(this, 0, fCopyProgramArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
4192 0);
bsalomond6246342015-06-04 13:57:00 -07004193 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07004194
4195 // dst rect edges in NDC (-1 to 1)
4196 int dw = dst->width();
4197 int dh = dst->height();
4198 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4199 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4200 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4201 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
4202 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4203 dy0 = -dy0;
4204 dy1 = -dy1;
4205 }
4206
bsalomone5286e02016-01-14 09:24:09 -08004207 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4208 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4209 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4210 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07004211 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07004212 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08004213 sy0 = sh - sy0;
4214 sy1 = sh - sy1;
4215 }
4216 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
4217 GrGLenum srcTarget = srcTex->target();
4218 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
4219 int sw = src->width();
4220 sx0 /= sw;
4221 sx1 /= sw;
4222 sy0 /= sh;
4223 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004224 }
4225
bsalomon7ea33f52015-11-22 14:51:00 -08004226 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4227 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4228 sx1 - sx0, sy1 - sy0, sx0, sy0));
4229 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004230
4231 GrXferProcessor::BlendInfo blendInfo;
4232 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004233 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004234 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004235 this->flushDrawFace(GrDrawFace::kBoth);
bsalomon083617b2016-02-12 12:10:14 -08004236 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004237 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004238 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004239 GrStencilSettings stencil;
4240 stencil.setDisabled();
4241 this->flushStencil(stencil);
4242
4243 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon083617b2016-02-12 12:10:14 -08004244 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, dst);
4245 this->didWriteToSurface(dst, &dstRect);
4246
cdaltone2e71c22016-04-07 18:13:29 -07004247 return true;
bsalomon6df86402015-06-01 10:41:49 -07004248}
4249
4250void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
4251 GrSurface* src,
4252 const SkIRect& srcRect,
4253 const SkIPoint& dstPoint) {
4254 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07004255 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07004256 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004257 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004258 SkASSERT(dstTex);
4259 // We modified the bound FBO
4260 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4261 GrGLIRect srcGLRect;
4262 srcGLRect.setRelativeTo(srcVP,
4263 srcRect.fLeft,
4264 srcRect.fTop,
4265 srcRect.width(),
4266 srcRect.height(),
4267 src->origin());
4268
4269 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004270 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004271 GrGLint dstY;
4272 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4273 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4274 } else {
4275 dstY = dstPoint.fY;
4276 }
bsalomon10528f12015-10-14 12:54:52 -07004277 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004278 dstPoint.fX, dstY,
4279 srcGLRect.fLeft, srcGLRect.fBottom,
4280 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07004281 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004282 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4283 srcRect.width(), srcRect.height());
4284 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004285}
4286
4287bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
4288 GrSurface* src,
4289 const SkIRect& srcRect,
4290 const SkIPoint& dstPoint) {
4291 SkASSERT(can_blit_framebuffer(dst, src, this));
4292 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4293 srcRect.width(), srcRect.height());
4294 if (dst == src) {
4295 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4296 return false;
mtklein404b3b22015-05-18 09:29:10 -07004297 }
bsalomon5df6fee2015-05-18 06:26:15 -07004298 }
bsalomon6df86402015-06-01 10:41:49 -07004299
bsalomon6df86402015-06-01 10:41:49 -07004300 GrGLIRect dstVP;
4301 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07004302 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4303 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004304 // We modified the bound FBO
4305 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4306 GrGLIRect srcGLRect;
4307 GrGLIRect dstGLRect;
4308 srcGLRect.setRelativeTo(srcVP,
4309 srcRect.fLeft,
4310 srcRect.fTop,
4311 srcRect.width(),
4312 srcRect.height(),
4313 src->origin());
4314 dstGLRect.setRelativeTo(dstVP,
4315 dstRect.fLeft,
4316 dstRect.fTop,
4317 dstRect.width(),
4318 dstRect.height(),
4319 dst->origin());
4320
4321 // BlitFrameBuffer respects the scissor, so disable it.
4322 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004323 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004324
4325 GrGLint srcY0;
4326 GrGLint srcY1;
4327 // Does the blit need to y-mirror or not?
4328 if (src->origin() == dst->origin()) {
4329 srcY0 = srcGLRect.fBottom;
4330 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4331 } else {
4332 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4333 srcY1 = srcGLRect.fBottom;
4334 }
4335 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4336 srcY0,
4337 srcGLRect.fLeft + srcGLRect.fWidth,
4338 srcY1,
4339 dstGLRect.fLeft,
4340 dstGLRect.fBottom,
4341 dstGLRect.fLeft + dstGLRect.fWidth,
4342 dstGLRect.fBottom + dstGLRect.fHeight,
4343 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07004344 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
4345 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004346 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004347 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004348}
4349
brianosman33f6b3f2016-06-02 05:49:21 -07004350// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4351// Uses draw calls to do a series of downsample operations to successive mips.
4352// If this returns false, then the calling code falls back to using glGenerateMipmap.
4353bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
brianosman09563ce2016-06-02 08:59:34 -07004354 // Our iterative downsample requires the ability to limit which level we're sampling:
4355 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004356 return false;
4357 }
4358
4359 // Mipmaps are only supported on 2D textures:
4360 if (GR_GL_TEXTURE_2D != texture->target()) {
4361 return false;
4362 }
4363
4364 // We need to be able to render to the texture for this to work:
4365 if (!this->caps()->isConfigRenderable(texture->config(), false)) {
4366 return false;
4367 }
4368
brianosman33f6b3f2016-06-02 05:49:21 -07004369 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4370 if (GrPixelConfigIsSRGB(texture->config())) {
4371 // If we have write-control, just set the state that we want:
4372 if (this->glCaps().srgbWriteControl()) {
4373 this->flushFramebufferSRGB(gammaCorrect);
4374 } else if (!gammaCorrect) {
4375 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4376 return false;
4377 }
4378 }
4379
4380 int width = texture->width();
4381 int height = texture->height();
4382 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4383
4384 // Define all mips, if we haven't previously done so:
4385 if (0 == texture->texturePriv().maxMipMapLevel()) {
4386 GrGLenum internalFormat;
4387 GrGLenum externalFormat;
4388 GrGLenum externalType;
4389 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4390 &internalFormat, &externalFormat, &externalType)) {
4391 return false;
4392 }
4393
4394 for (GrGLint level = 1; level < levelCount; ++level) {
4395 // Define the next mip:
4396 width = SkTMax(1, width / 2);
4397 height = SkTMax(1, height / 2);
4398 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4399 width, height, 0,
4400 externalFormat, externalType, nullptr));
4401 }
4402 }
4403
4404 // Create (if necessary), then bind temporary FBO:
4405 if (0 == fTempDstFBOID) {
4406 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4407 }
4408 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
4409 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4410
4411 // Bind the texture, to get things configured for filtering.
4412 // We'll be changing our base level further below:
4413 this->setTextureUnit(0);
4414 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
4415 this->bindTexture(0, params, gammaCorrect, texture);
4416
4417 // Vertex data:
4418 if (!fMipmapProgramArrayBuffer) {
4419 static const GrGLfloat vdata[] = {
4420 0, 0,
4421 0, 1,
4422 1, 0,
4423 1, 1
4424 };
4425 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4426 kVertex_GrBufferType,
4427 kStatic_GrAccessPattern, vdata));
4428 }
4429 if (!fMipmapProgramArrayBuffer) {
4430 return false;
4431 }
4432
4433 fHWVertexArrayState.setVertexArrayID(this, 0);
4434
4435 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
4436 attribs->set(this, 0, fMipmapProgramArrayBuffer, kVec2f_GrVertexAttribType,
4437 2 * sizeof(GrGLfloat), 0);
4438 attribs->disableUnusedArrays(this, 0x1);
4439
4440 // Set "simple" state once:
4441 GrXferProcessor::BlendInfo blendInfo;
4442 blendInfo.reset();
4443 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4444 this->flushColorWrite(true);
robertphillips5fa7f302016-07-21 09:21:04 -07004445 this->flushDrawFace(GrDrawFace::kBoth);
brianosman33f6b3f2016-06-02 05:49:21 -07004446 this->flushHWAAState(nullptr, false, false);
4447 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004448 this->disableWindowRectangles();
brianosman33f6b3f2016-06-02 05:49:21 -07004449 GrStencilSettings stencil;
4450 stencil.setDisabled();
4451 this->flushStencil(stencil);
4452
4453 // Do all the blits:
4454 width = texture->width();
4455 height = texture->height();
4456 GrGLIRect viewport;
4457 viewport.fLeft = 0;
4458 viewport.fBottom = 0;
4459 for (GrGLint level = 1; level < levelCount; ++level) {
4460 // Get and bind the program for this particular downsample (filter shape can vary):
4461 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4462 if (!fMipmapPrograms[progIdx].fProgram) {
4463 if (!this->createMipmapProgram(progIdx)) {
4464 SkDebugf("Failed to create mipmap program.\n");
4465 return false;
4466 }
4467 }
4468 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4469 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4470
4471 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4472 const float invWidth = 1.0f / width;
4473 const float invHeight = 1.0f / height;
4474 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4475 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4476 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4477
4478 // Only sample from previous mip
4479 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4480
4481 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4482 GR_GL_TEXTURE_2D, texture->textureID(), level));
4483
4484 width = SkTMax(1, width / 2);
4485 height = SkTMax(1, height / 2);
4486 viewport.fWidth = width;
4487 viewport.fHeight = height;
4488 this->flushViewport(viewport);
4489
4490 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4491 }
4492
4493 // Unbind:
4494 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4495 GR_GL_TEXTURE_2D, 0, 0));
4496
4497 return true;
4498}
4499
csmartdalton0d28e572016-07-06 09:59:43 -07004500void GrGLGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
4501 int* effectiveSampleCnt, SamplePattern* samplePattern) {
csmartdaltonf9635992016-08-10 11:09:07 -07004502 SkASSERT(!rt->isMixedSampled() || rt->renderTargetPriv().getStencilAttachment() ||
cdalton28f45b92016-03-07 13:58:26 -08004503 stencil.isDisabled());
4504
4505 this->flushStencil(stencil);
4506 this->flushHWAAState(rt, true, !stencil.isDisabled());
4507 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4508
4509 if (0 != this->caps()->maxRasterSamples()) {
4510 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4511 } else {
4512 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4513 }
4514
4515 SkASSERT(*effectiveSampleCnt >= rt->desc().fSampleCnt);
4516
4517 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004518 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004519 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4520 GrGLfloat pos[2];
4521 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4522 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004523 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004524 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004525 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004526 }
4527 }
4528 }
4529}
4530
cdalton231c5fd2015-05-13 12:35:36 -07004531void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004532 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004533 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004534 case kTexture_GrXferBarrierType: {
4535 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4536 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4537 // The render target uses separate storage so no need for glTextureBarrier.
4538 // FIXME: The render target will resolve automatically when its texture is bound,
4539 // but we could resolve only the bounds that will be read if we do it here instead.
4540 return;
4541 }
cdalton9954bc32015-04-29 14:17:00 -07004542 SkASSERT(this->caps()->textureBarrierSupport());
4543 GL_CALL(TextureBarrier());
4544 return;
cdalton231c5fd2015-05-13 12:35:36 -07004545 }
cdalton8917d622015-05-06 13:40:21 -07004546 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004547 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004548 this->caps()->blendEquationSupport());
4549 GL_CALL(BlendBarrier());
4550 return;
bsalomoncb02b382015-08-12 11:14:50 -07004551 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004552 }
4553}
4554
jvanverth88957922015-07-14 11:02:52 -07004555GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004556 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004557 if (!this->caps()->isConfigTexturable(config)) {
4558 return false;
4559 }
bsalomon091f60c2015-11-10 11:54:56 -08004560 GrGLTextureInfo* info = new GrGLTextureInfo;
4561 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004562 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004563 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004564 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4565 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004566 GL_CALL(BindTexture(info->fTarget, info->fID));
bsalomone63ffef2016-02-05 07:17:34 -08004567 fHWBoundTextureUniqueIDs[0] = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004568 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4569 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4570 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4571 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004572
bsalomon76148af2016-01-12 11:13:47 -08004573 GrGLenum internalFormat;
4574 GrGLenum externalFormat;
4575 GrGLenum externalType;
4576
4577 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4578 &externalType)) {
4579 delete info;
4580#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4581 return 0;
4582#else
4583 return reinterpret_cast<GrBackendObject>(nullptr);
4584#endif
4585 }
jvanverth672bb7f2015-07-13 07:19:57 -07004586
bsalomon091f60c2015-11-10 11:54:56 -08004587 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004588 externalType, pixels));
4589
bsalomon091f60c2015-11-10 11:54:56 -08004590#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4591 GrGLuint id = info->fID;
4592 delete info;
4593 return id;
4594#else
4595 return reinterpret_cast<GrBackendObject>(info);
4596#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004597}
4598
jvanverth88957922015-07-14 11:02:52 -07004599bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004600#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004601 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004602#else
4603 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
4604#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004605
4606 GrGLboolean result;
4607 GL_CALL_RET(result, IsTexture(texID));
4608
4609 return (GR_GL_TRUE == result);
4610}
4611
bsalomone63ffef2016-02-05 07:17:34 -08004612void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
bsalomon091f60c2015-11-10 11:54:56 -08004613#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004614 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004615#else
4616 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
4617 GrGLuint texID = info->fID;
4618#endif
4619
bsalomon67d76202015-11-11 12:40:42 -08004620 if (!abandonTexture) {
4621 GL_CALL(DeleteTextures(1, &texID));
4622 }
bsalomon091f60c2015-11-10 11:54:56 -08004623
4624#ifndef SK_IGNORE_GL_TEXTURE_TARGET
4625 delete info;
4626#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004627}
4628
joshualitt8fd844f2015-12-02 13:36:47 -08004629void GrGLGpu::resetShaderCacheForTesting() const {
4630 fProgramCache->abandon();
4631}
4632
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004633///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004634
cdaltone2e71c22016-04-07 18:13:29 -07004635GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004636 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004637 GrGLAttribArrayState* attribState;
4638
cdaltone2e71c22016-04-07 18:13:29 -07004639 if (gpu->glCaps().isCoreProfile()) {
4640 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004641 GrGLuint arrayID;
4642 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4643 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004644 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004645 }
cdaltone2e71c22016-04-07 18:13:29 -07004646 if (ibuf) {
4647 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004648 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004649 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004650 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004651 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004652 if (ibuf) {
4653 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4654 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004655 } else {
4656 this->setVertexArrayID(gpu, 0);
4657 }
4658 int attrCount = gpu->glCaps().maxVertexAttributes();
4659 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4660 fDefaultVertexArrayAttribState.resize(attrCount);
4661 }
4662 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004663 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004664 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004665}
bsalomone179a912016-01-20 06:18:10 -08004666
4667bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
4668 GrTextureProducer::CopyParams* copyParams) const {
4669 if (textureParams.isTiled() ||
4670 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
4671 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
4672 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4673 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
4674 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
4675 copyParams->fWidth = texture->width();
4676 copyParams->fHeight = texture->height();
4677 return true;
4678 }
4679 }
4680 return false;
4681}