blob: e8b575584933a19ad8e89d03eb510f8936a52244 [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"
egdaniel8dc7c3a2015-04-16 11:22:42 -070011#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080012#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070014#include "GrMesh.h"
egdaniel8dd688b2015-01-22 10:16:09 -080015#include "GrPipeline.h"
ethannicholas22793252016-01-30 09:59:10 -080016#include "GrPLSGeometryProcessor.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000020#include "GrTypes.h"
bsalomon6df86402015-06-01 10:41:49 -070021#include "builders/GrGLShaderStringBuilder.h"
egdanielcb7ba1e2015-10-26 08:38:25 -070022#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070023#include "glsl/GrGLSLCaps.h"
ethannicholas22793252016-01-30 09:59:10 -080024#include "glsl/GrGLSLPLSPathRendering.h"
cblume55f2d2d2016-02-26 13:20:48 -080025#include "SkMipMap.h"
26#include "SkPixmap.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000027#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000028#include "SkTemplates.h"
cblume55f2d2d2016-02-26 13:20:48 -080029#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000030
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000032#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000033
reed@google.comac10a2d2010-12-22 21:39:39 +000034#define SKIP_CACHE_CHECK true
35
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000036#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
37 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
38 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
39 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000041 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
42 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
43 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
44#endif
45
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000046///////////////////////////////////////////////////////////////////////////////
47
cdaltonb85a0aa2014-07-21 15:32:44 -070048
cdalton8917d622015-05-06 13:40:21 -070049static const GrGLenum gXfermodeEquation2Blend[] = {
50 // Basic OpenGL blend equations.
51 GR_GL_FUNC_ADD,
52 GR_GL_FUNC_SUBTRACT,
53 GR_GL_FUNC_REVERSE_SUBTRACT,
54
55 // GL_KHR_blend_equation_advanced.
56 GR_GL_SCREEN,
57 GR_GL_OVERLAY,
58 GR_GL_DARKEN,
59 GR_GL_LIGHTEN,
60 GR_GL_COLORDODGE,
61 GR_GL_COLORBURN,
62 GR_GL_HARDLIGHT,
63 GR_GL_SOFTLIGHT,
64 GR_GL_DIFFERENCE,
65 GR_GL_EXCLUSION,
66 GR_GL_MULTIPLY,
67 GR_GL_HSL_HUE,
68 GR_GL_HSL_SATURATION,
69 GR_GL_HSL_COLOR,
70 GR_GL_HSL_LUMINOSITY
71};
72GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
73GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
74GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
75GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
76GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
77GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
78GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
79GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
80GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
81GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
82GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
83GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
84GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
85GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
86GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
87GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
88GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
89GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070090GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070091
twiz@google.com0f31ca72011-03-18 17:38:11 +000092static const GrGLenum gXfermodeCoeff2Blend[] = {
93 GR_GL_ZERO,
94 GR_GL_ONE,
95 GR_GL_SRC_COLOR,
96 GR_GL_ONE_MINUS_SRC_COLOR,
97 GR_GL_DST_COLOR,
98 GR_GL_ONE_MINUS_DST_COLOR,
99 GR_GL_SRC_ALPHA,
100 GR_GL_ONE_MINUS_SRC_ALPHA,
101 GR_GL_DST_ALPHA,
102 GR_GL_ONE_MINUS_DST_ALPHA,
103 GR_GL_CONSTANT_COLOR,
104 GR_GL_ONE_MINUS_CONSTANT_COLOR,
105 GR_GL_CONSTANT_ALPHA,
106 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000107
108 // extended blend coeffs
109 GR_GL_SRC1_COLOR,
110 GR_GL_ONE_MINUS_SRC1_COLOR,
111 GR_GL_SRC1_ALPHA,
112 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000113};
114
bsalomon861e1032014-12-16 07:33:49 -0800115bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000116 static const bool gCoeffReferencesBlendConst[] = {
117 false,
118 false,
119 false,
120 false,
121 false,
122 false,
123 false,
124 false,
125 false,
126 false,
127 true,
128 true,
129 true,
130 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000131
132 // extended blend coeffs
133 false,
134 false,
135 false,
136 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000137 };
138 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700139 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000140
bsalomon@google.com47059542012-06-06 20:51:20 +0000141 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
142 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
143 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
144 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
145 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
146 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
147 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
148 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
149 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
150 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
151 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
152 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
153 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
154 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155
bsalomon@google.com47059542012-06-06 20:51:20 +0000156 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
157 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
158 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
159 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000160
161 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700162 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000163}
164
reed@google.comac10a2d2010-12-22 21:39:39 +0000165///////////////////////////////////////////////////////////////////////////////
166
egdanielff1d5472015-09-10 08:37:20 -0700167
bsalomon682c2692015-05-22 14:01:46 -0700168GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
169 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700170 SkAutoTUnref<const GrGLInterface> glInterface(
171 reinterpret_cast<const GrGLInterface*>(backendContext));
172 if (!glInterface) {
173 glInterface.reset(GrGLDefaultInterface());
174 } else {
175 glInterface->ref();
176 }
177 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700178 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700179 }
bsalomon682c2692015-05-22 14:01:46 -0700180 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700181 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700182 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700183 }
halcanary96fcdcc2015-08-27 07:41:13 -0700184 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700185}
186
rileya@google.come38160c2012-07-03 18:03:04 +0000187static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000188
bsalomon424cc262015-05-22 10:37:30 -0700189GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000190 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700191 , fGLContext(ctx)
192 , fProgramCache(new ProgramCache(this))
193 , fHWProgramID(0)
194 , fTempSrcFBOID(0)
195 , fTempDstFBOID(0)
196 , fStencilClearFBOID(0)
197 , fHWPLSEnabled(false)
198 , fPLSHasBeenUsed(false)
199 , fHWMinSampleShading(0.0) {
200 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
201 fCopyPrograms[i].fProgram = 0;
202 }
203 fWireRectProgram.fProgram = 0;
204 fPLSSetupProgram.fProgram = 0;
205
bsalomon424cc262015-05-22 10:37:30 -0700206 SkASSERT(ctx);
207 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000208
cdalton9c3f1432016-03-11 10:07:37 -0800209 fHWBoundTextureUniqueIDs.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000210
cdaltone2e71c22016-04-07 18:13:29 -0700211 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
212 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
213 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
214 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
215 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
216 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
217 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
218 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
219 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
220 } else {
221 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
222 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
223 }
224 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
225
226 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
227 fPathRendering.reset(new GrGLPathRendering(this));
228 }
229
bsalomon424cc262015-05-22 10:37:30 -0700230 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000231 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000232 const GrGLubyte* vendor;
233 const GrGLubyte* renderer;
234 const GrGLubyte* version;
235 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
236 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
237 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800238 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000239 this);
tfarina38406c82014-10-31 07:11:12 -0700240 SkDebugf("------ VENDOR %s\n", vendor);
241 SkDebugf("------ RENDERER %s\n", renderer);
242 SkDebugf("------ VERSION %s\n", version);
243 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700244 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700245 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800246 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000247 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000248}
249
bsalomon861e1032014-12-16 07:33:49 -0800250GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700251 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
252 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800253 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700254 fCopyProgramArrayBuffer.reset();
255 fWireRectArrayBuffer.reset();
256 fPLSSetupProgram.fArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800257
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000258 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000259 // detach the current program so there is no confusion on OpenGL's part
260 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000261 GL_CALL(UseProgram(0));
262 }
263
egdanield803f272015-03-18 13:01:52 -0700264 if (0 != fTempSrcFBOID) {
265 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800266 }
egdanield803f272015-03-18 13:01:52 -0700267 if (0 != fTempDstFBOID) {
268 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800269 }
egdanield803f272015-03-18 13:01:52 -0700270 if (0 != fStencilClearFBOID) {
271 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800272 }
egdaniel0f5f9672015-02-03 11:10:51 -0800273
bsalomon7ea33f52015-11-22 14:51:00 -0800274 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
275 if (0 != fCopyPrograms[i].fProgram) {
276 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
277 }
bsalomon6df86402015-06-01 10:41:49 -0700278 }
bsalomon6dea83f2015-12-03 12:58:06 -0800279
bsalomon6dea83f2015-12-03 12:58:06 -0800280 if (0 != fWireRectProgram.fProgram) {
281 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
282 }
283
ethannicholas22793252016-01-30 09:59:10 -0800284 if (0 != fPLSSetupProgram.fProgram) {
285 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
286 }
287
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000288 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700289}
290
cdaltone2e71c22016-04-07 18:13:29 -0700291bool GrGLGpu::createPLSSetupProgram() {
292 if (!fPLSSetupProgram.fArrayBuffer) {
293 static const GrGLfloat vdata[] = {
294 0, 0,
295 0, 1,
296 1, 0,
297 1, 1
298 };
299 fPLSSetupProgram.fArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
300 kVertex_GrBufferType,
301 kStatic_GrAccessPattern, vdata));
302 if (!fPLSSetupProgram.fArrayBuffer) {
303 return false;
304 }
305 }
306
307 SkASSERT(!fPLSSetupProgram.fProgram);
308 GL_CALL_RET(fPLSSetupProgram.fProgram, CreateProgram());
309 if (!fPLSSetupProgram.fProgram) {
310 return false;
311 }
312
cdaltonc08f1962016-02-12 12:14:06 -0800313 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
314 const char* version = glslCaps->versionDeclString();
ethannicholas22793252016-01-30 09:59:10 -0800315
316 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
317 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
318 GrShaderVar::kUniform_TypeModifier);
319 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
320 GrGLSLShaderVar uTexture("u_texture", kSampler2D_GrSLType, GrShaderVar::kUniform_TypeModifier);
321 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
halcanary9d524f22016-03-29 09:03:52 -0700322
ethannicholas22793252016-01-30 09:59:10 -0800323 SkString vshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800324 if (glslCaps->noperspectiveInterpolationSupport()) {
325 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
326 vshaderTxt.appendf("#extension %s : require\n", extension);
327 }
328 vTexCoord.addModifier("noperspective");
329 }
330 aVertex.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800331 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800332 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800333 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800334 uPosXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800335 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800336 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800337 vshaderTxt.append(";");
halcanary9d524f22016-03-29 09:03:52 -0700338
ethannicholas22793252016-01-30 09:59:10 -0800339 vshaderTxt.append(
340 "// PLS Setup Program VS\n"
341 "void main() {"
342 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
343 " gl_Position.zw = vec2(0, 1);"
344 "}"
345 );
346
347 SkString fshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800348 if (glslCaps->noperspectiveInterpolationSupport()) {
349 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
350 fshaderTxt.appendf("#extension %s : require\n", extension);
351 }
352 }
ethannicholas22793252016-01-30 09:59:10 -0800353 fshaderTxt.append("#extension ");
cdaltonc08f1962016-02-12 12:14:06 -0800354 fshaderTxt.append(glslCaps->fbFetchExtensionString());
ethannicholas22793252016-01-30 09:59:10 -0800355 fshaderTxt.append(" : require\n");
356 fshaderTxt.append("#extension GL_EXT_shader_pixel_local_storage : require\n");
cdaltonc08f1962016-02-12 12:14:06 -0800357 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800358 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
cdaltonc08f1962016-02-12 12:14:06 -0800359 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800360 fshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800361 uTexture.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800362 fshaderTxt.append(";");
363
364 fshaderTxt.appendf(
365 "// PLS Setup Program FS\n"
366 GR_GL_PLS_PATH_DATA_DECL
367 "void main() {\n"
368 " " GR_GL_PLS_DSTCOLOR_NAME " = gl_LastFragColorARM;\n"
369 " pls.windings = ivec4(0, 0, 0, 0);\n"
370 "}"
371 );
cdaltone2e71c22016-04-07 18:13:29 -0700372
ethannicholas22793252016-01-30 09:59:10 -0800373 const char* str;
374 GrGLint length;
375
376 str = vshaderTxt.c_str();
377 length = SkToInt(vshaderTxt.size());
378 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
379 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
380
381 str = fshaderTxt.c_str();
382 length = SkToInt(fshaderTxt.size());
383 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
384 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
385
386 GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
387
388 GL_CALL_RET(fPLSSetupProgram.fPosXformUniform, GetUniformLocation(fPLSSetupProgram.fProgram,
389 "u_posXform"));
390
391 GL_CALL(BindAttribLocation(fPLSSetupProgram.fProgram, 0, "a_vertex"));
392
393 GL_CALL(DeleteShader(vshader));
394 GL_CALL(DeleteShader(fshader));
395
cdaltone2e71c22016-04-07 18:13:29 -0700396 return true;
ethannicholas22793252016-01-30 09:59:10 -0800397}
398
bsalomon6e2aad42016-04-01 11:54:31 -0700399void GrGLGpu::disconnect(DisconnectType type) {
400 INHERITED::disconnect(type);
401 if (DisconnectType::kCleanup == type) {
402 if (fHWProgramID) {
403 GL_CALL(UseProgram(0));
404 }
405 if (fTempSrcFBOID) {
406 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
407 }
408 if (fTempDstFBOID) {
409 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
410 }
411 if (fStencilClearFBOID) {
412 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
413 }
bsalomon6e2aad42016-04-01 11:54:31 -0700414 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
415 if (fCopyPrograms[i].fProgram) {
416 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
417 }
418 }
419 if (fWireRectProgram.fProgram) {
420 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
421 }
bsalomon6e2aad42016-04-01 11:54:31 -0700422 if (fPLSSetupProgram.fProgram) {
423 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
424 }
bsalomon6e2aad42016-04-01 11:54:31 -0700425 } else {
426 if (fProgramCache) {
427 fProgramCache->abandon();
428 }
429 }
430
431 delete fProgramCache;
432 fProgramCache = nullptr;
433
bsalomonc8dc1f72014-08-21 13:02:13 -0700434 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700435 fTempSrcFBOID = 0;
436 fTempDstFBOID = 0;
437 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700438 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800439 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
440 fCopyPrograms[i].fProgram = 0;
441 }
bsalomon6dea83f2015-12-03 12:58:06 -0800442 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700443 fWireRectArrayBuffer.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700444 fPLSSetupProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700445 fPLSSetupProgram.fArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700446 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700447 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700448 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000449}
450
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000451///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000452
bsalomon861e1032014-12-16 07:33:49 -0800453void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000454 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000455 if (resetBits & kMisc_GrGLBackendState) {
456 GL_CALL(Disable(GR_GL_DEPTH_TEST));
457 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000458
cdaltone2e71c22016-04-07 18:13:29 -0700459 fHWBufferState[kTexel_GrBufferType].invalidate();
460 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
461 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
462 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700463
egdaniel8dd688b2015-01-22 10:16:09 -0800464 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000466 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000467 // Desktop-only state that we never change
468 if (!this->glCaps().isCoreProfile()) {
469 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
470 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
471 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
472 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
473 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
474 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
475 }
476 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
477 // core profile. This seems like a bug since the core spec removes any mention of
478 // GL_ARB_imaging.
479 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
480 GL_CALL(Disable(GR_GL_COLOR_TABLE));
481 }
482 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
483 // Since ES doesn't support glPointSize at all we always use the VS to
484 // set the point size
485 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
486
487 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
488 // currently part of our gl interface. There are probably others as
489 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000490 }
joshualitt58162332014-08-01 06:44:53 -0700491
492 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700493 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700494 // The arm extension requires specifically enabling MSAA fetching per sample.
495 // On some devices this may have a perf hit. Also multiple render targets are disabled
496 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
497 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000498 fHWWriteToColor = kUnknown_TriState;
499 // we only ever use lines in hairline mode
500 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700501 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000502 }
edisonn@google.comba669992013-06-28 16:03:21 +0000503
egdanielb414f252014-07-29 13:15:47 -0700504 if (resetBits & kMSAAEnable_GrGLBackendState) {
505 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700506
egdanieleed519e2016-01-15 11:36:18 -0800507 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800508 if (0 != this->caps()->maxRasterSamples()) {
509 fHWRasterMultisampleEnabled = kUnknown_TriState;
510 fHWNumRasterSamples = 0;
511 }
512
513 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
514 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700515 GL_CALL(CoverageModulation(GR_GL_RGBA));
516 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000517 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000519 fHWActiveTextureUnitIdx = -1; // invalid
520
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700522 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
523 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000524 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000525 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000526
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000527 if (resetBits & kBlend_GrGLBackendState) {
528 fHWBlendState.invalidate();
529 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000530
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000531 if (resetBits & kView_GrGLBackendState) {
532 fHWScissorSettings.invalidate();
533 fHWViewport.invalidate();
534 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 if (resetBits & kStencil_GrGLBackendState) {
537 fHWStencilSettings.invalidate();
538 fHWStencilTestEnabled = kUnknown_TriState;
539 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000540
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000541 // Vertex
542 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700543 fHWVertexArrayState.invalidate();
544 fHWBufferState[kVertex_GrBufferType].invalidate();
545 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000546 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000548 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700549 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700550 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000551 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000552
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000553 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700554 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700555 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000556 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000557 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000558
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000559 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000560 if (resetBits & kPixelStore_GrGLBackendState) {
561 if (this->glCaps().unpackRowLengthSupport()) {
562 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
563 }
564 if (this->glCaps().packRowLengthSupport()) {
565 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
566 }
567 if (this->glCaps().unpackFlipYSupport()) {
568 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
569 }
570 if (this->glCaps().packFlipYSupport()) {
571 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
572 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000573 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000574
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000575 if (resetBits & kProgram_GrGLBackendState) {
576 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000577 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000578}
579
egdanielcf614fd2015-04-22 13:58:58 -0700580static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000581 // By default, GrRenderTargets are GL's normal orientation so that they
582 // can be drawn to by the outside world without the client having
583 // to render upside down.
584 if (kDefault_GrSurfaceOrigin == origin) {
585 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
586 } else {
587 return origin;
588 }
589}
590
bsalomon6dc6f5f2015-06-18 09:12:16 -0700591GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
592 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800593#ifdef SK_IGNORE_GL_TEXTURE_TARGET
594 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700595 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000596 }
bsalomon091f60c2015-11-10 11:54:56 -0800597#else
598 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
599 if (!info || !info->fID) {
600 return nullptr;
601 }
602#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000603
bsalomon7ea33f52015-11-22 14:51:00 -0800604 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
605 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
606
bsalomonb15b4c12014-10-29 12:41:57 -0700607 GrGLTexture::IDDesc idDesc;
608 GrSurfaceDesc surfDesc;
609
bsalomon091f60c2015-11-10 11:54:56 -0800610#ifdef SK_IGNORE_GL_TEXTURE_TARGET
611 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
cblume55f2d2d2016-02-26 13:20:48 -0800612 // When we create the texture, we only
613 // create GL_TEXTURE_2D at the moment.
614 // External clients can do something different.
615
bsalomon091f60c2015-11-10 11:54:56 -0800616 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
617#else
618 idDesc.fInfo = *info;
619#endif
bsalomone5286e02016-01-14 09:24:09 -0800620
bsalomon7ea33f52015-11-22 14:51:00 -0800621 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
622 if (renderTarget) {
623 // This combination is not supported.
624 return nullptr;
625 }
cdalton9c3f1432016-03-11 10:07:37 -0800626 if (!this->glCaps().glslCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800627 return nullptr;
628 }
bsalomone5286e02016-01-14 09:24:09 -0800629 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
630 if (!this->glCaps().rectangleTextureSupport()) {
631 return nullptr;
632 }
633 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
634 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800635 }
bsalomone5286e02016-01-14 09:24:09 -0800636
637 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800638 // for a render buffer that resolves to the texture. We don't support MSAA textures.
639 if (desc.fSampleCnt && !renderTarget) {
640 return nullptr;
641 }
bsalomon10528f12015-10-14 12:54:52 -0700642
bsalomon6dc6f5f2015-06-18 09:12:16 -0700643 switch (ownership) {
644 case kAdopt_GrWrapOwnership:
645 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
646 break;
647 case kBorrow_GrWrapOwnership:
648 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
649 break;
bsalomone5286e02016-01-14 09:24:09 -0800650 }
bsalomonb15b4c12014-10-29 12:41:57 -0700651
bsalomonb15b4c12014-10-29 12:41:57 -0700652 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
653 surfDesc.fWidth = desc.fWidth;
654 surfDesc.fHeight = desc.fHeight;
655 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700656 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000657 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
658 // assuming the old behaviour, which is that backend textures are always
659 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
660 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
661 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700662 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000663 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700664 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000665 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000666
halcanary96fcdcc2015-08-27 07:41:13 -0700667 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000668 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700669 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700670 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800671 idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700672 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000673 }
halcanary385fe4d2015-08-26 13:07:48 -0700674 texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000675 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700676 texture = new GrGLTexture(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000677 }
halcanary96fcdcc2015-08-27 07:41:13 -0700678 if (nullptr == texture) {
679 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000680 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000681
bsalomon@google.come269f212011-11-07 13:29:52 +0000682 return texture;
683}
684
bsalomon6dc6f5f2015-06-18 09:12:16 -0700685GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
686 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700687 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700688 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700689 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700690 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700691 switch (ownership) {
692 case kAdopt_GrWrapOwnership:
693 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
694 break;
695 case kBorrow_GrWrapOwnership:
696 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
697 break;
cblume61214052016-01-26 09:10:48 -0800698 }
senorblancod7395d82015-06-18 13:26:52 -0700699 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000700
bsalomonb15b4c12014-10-29 12:41:57 -0700701 GrSurfaceDesc desc;
702 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700703 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700704 desc.fWidth = wrapDesc.fWidth;
705 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700706 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700707 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
708
egdanielec00d942015-09-14 12:56:10 -0700709 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000710}
711
kkinnunen49c4c222016-04-01 04:50:37 -0700712GrRenderTarget* GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800713#ifdef SK_IGNORE_GL_TEXTURE_TARGET
714 if (!desc.fTextureHandle) {
715 return nullptr;
716 }
717#else
718 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
719 if (!info || !info->fID) {
720 return nullptr;
721 }
722#endif
723
kkinnunen49c4c222016-04-01 04:50:37 -0700724 GrGLTextureInfo texInfo;
ericrkf7b8b8a2016-02-24 14:49:51 -0800725 GrSurfaceDesc surfDesc;
726
727#ifdef SK_IGNORE_GL_TEXTURE_TARGET
kkinnunen49c4c222016-04-01 04:50:37 -0700728 texInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
ericrkf7b8b8a2016-02-24 14:49:51 -0800729 // We only support GL_TEXTURE_2D at the moment.
kkinnunen49c4c222016-04-01 04:50:37 -0700730 texInfo.fTarget = GR_GL_TEXTURE_2D;
ericrkf7b8b8a2016-02-24 14:49:51 -0800731#else
kkinnunen49c4c222016-04-01 04:50:37 -0700732 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800733#endif
734
kkinnunen49c4c222016-04-01 04:50:37 -0700735 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
736 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800737 // Only texture rectangle and texture 2d are supported. We do not check whether texture
738 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
739 // we assume the necessary support exists.
740 return nullptr;
741 }
742
ericrkf7b8b8a2016-02-24 14:49:51 -0800743 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
744 surfDesc.fWidth = desc.fWidth;
745 surfDesc.fHeight = desc.fHeight;
746 surfDesc.fConfig = desc.fConfig;
747 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
748 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
749 // assuming the old behaviour, which is that backend textures are always
750 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
751 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
752 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
753 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
754 } else {
755 surfDesc.fOrigin = desc.fOrigin;
756 }
757
758 GrGLRenderTarget::IDDesc rtIDDesc;
759 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
kkinnunen49c4c222016-04-01 04:50:37 -0700760 texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800761 return nullptr;
762 }
763 return GrGLRenderTarget::CreateWrapped(this, surfDesc, rtIDDesc, 0);
764}
765
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000766////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800767
bsalomonf0674512015-07-28 13:26:15 -0700768bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800769 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700770 DrawPreference* drawPreference,
771 WritePixelTempDrawInfo* tempDrawInfo) {
772 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
773 return false;
774 }
775
bsalomon6cb3cbe2015-07-30 07:34:27 -0700776 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
777 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
778 if (!dstSurface->asTexture()) {
779 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800780 } else {
781 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800782 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
783 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800784 return false;
785 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700786 }
787
bsalomon16921ec2015-07-30 15:34:56 -0700788 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
789 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
790 }
791
bsalomon6c9cd552016-01-22 07:17:34 -0800792 // Start off assuming no swizzling
793 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
794 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700795
796 // These settings we will always want if a temp draw is performed. Initially set the config
797 // to srcConfig, though that may be modified if we decide to do a R/G swap.
798 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
799 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
800 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
801 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
802 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
803 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
804
805 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
806
807 if (configsAreRBSwaps) {
808 if (!this->caps()->isConfigTexturable(srcConfig)) {
809 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
810 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800811 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
812 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700813 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
814 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700815 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
816 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800817 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
818 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700819 } else if (kGLES_GrGLStandard == this->glStandard() &&
820 this->glCaps().bgraIsInternalFormat()) {
821 // The internal format and external formats must match texture uploads so we can't
822 // swizzle while uploading when BGRA is a distinct internal format.
823 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
824 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800825 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
826 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700827 }
828 }
829
830 if (!this->glCaps().unpackFlipYSupport() &&
831 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
832 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
833 }
834
835 return true;
836}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837
jvanverth17aa0472016-01-05 10:41:27 -0800838static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
839 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700840 if (!glTex) {
841 return false;
842 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000843
bsalomon16921ec2015-07-30 15:34:56 -0700844 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
845 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
846 return false;
847 }
848
bsalomone5286e02016-01-14 09:24:09 -0800849 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
850 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800851 return false;
852 }
853
jvanverth17aa0472016-01-05 10:41:27 -0800854 return true;
855}
856
857bool GrGLGpu::onWritePixels(GrSurface* surface,
858 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800859 GrPixelConfig config,
860 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800861 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
862
863 if (!check_write_and_transfer_input(glTex, surface, config)) {
864 return false;
865 }
866
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000867 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700868 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000869
krajcevski145d48c2014-06-11 16:07:50 -0700870 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700871 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800872 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700873 SkASSERT(config == glTex->desc().fConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800874 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800875 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700876 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800877 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800878 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700879 }
880
brianosman3ebd2762016-03-29 11:27:20 -0700881 if (success) {
882 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
883 this->didWriteToSurface(surface, &rect);
884 }
885
cblume55f2d2d2016-02-26 13:20:48 -0800886 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000887}
888
jvanverth17aa0472016-01-05 10:41:27 -0800889bool GrGLGpu::onTransferPixels(GrSurface* surface,
890 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700891 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800892 size_t offset, size_t rowBytes) {
893 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
894
895 if (!check_write_and_transfer_input(glTex, surface, config)) {
896 return false;
897 }
898
899 // For the moment, can't transfer compressed data
900 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
901 return false;
902 }
903
904 this->setScratchTextureUnit();
905 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
906
cdalton397536c2016-03-25 12:15:03 -0700907 SkASSERT(!transferBuffer->isMapped());
908 SkASSERT(!transferBuffer->isCPUBacked());
909 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700910 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800911
912 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800913 GrMipLevel mipLevel;
cdalton397536c2016-03-25 12:15:03 -0700914 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800915 mipLevel.fRowBytes = rowBytes;
916 SkSTArray<1, GrMipLevel> texels;
917 texels.push_back(mipLevel);
jcgregorioe7d7f902016-02-04 19:51:25 -0800918 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800919 left, top, width, height, config, texels);
jvanverth17aa0472016-01-05 10:41:27 -0800920 if (success) {
brianosman3ebd2762016-03-29 11:27:20 -0700921 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
922 this->didWriteToSurface(surface, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800923 return true;
924 }
925
926 return false;
927}
928
bsalomonf46a1242015-12-15 12:37:38 -0800929// For GL_[UN]PACK_ALIGNMENT.
930static inline GrGLint config_alignment(GrPixelConfig config) {
931 SkASSERT(!GrPixelConfigIsCompressed(config));
932 switch (config) {
933 case kAlpha_8_GrPixelConfig:
934 return 1;
935 case kRGB_565_GrPixelConfig:
936 case kRGBA_4444_GrPixelConfig:
937 case kAlpha_half_GrPixelConfig:
938 case kRGBA_half_GrPixelConfig:
939 return 2;
940 case kRGBA_8888_GrPixelConfig:
941 case kBGRA_8888_GrPixelConfig:
942 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700943 case kSBGRA_8888_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800944 case kRGBA_float_GrPixelConfig:
945 return 4;
946 default:
947 return 0;
948 }
949}
950
bsalomonb15b4c12014-10-29 12:41:57 -0700951static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
952 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700953 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000954 return GR_GL_GET_ERROR(interface);
955 } else {
956 return CHECK_ALLOC_ERROR(interface);
957 }
958}
959
cblume55f2d2d2016-02-26 13:20:48 -0800960/**
961 * Creates storage space for the texture and fills it with texels.
962 *
963 * @param desc The surface descriptor for the texture being created.
964 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800965 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -0800966 * @param internalFormat The data format used for the internal storage of the texture.
967 * @param externalFormat The data format used for the external storage of the texture.
968 * @param externalType The type of the data used for the external storage of the texture.
969 * @param texels The texel data of the texture being created.
970 * @param baseWidth The width of the texture's base mipmap level
971 * @param baseHeight The height of the texture's base mipmap level
972 * @param succeeded Set to true if allocating and populating the texture completed
973 * without error.
974 */
bsalomone699d0c2016-03-09 06:25:15 -0800975static bool allocate_and_populate_uncompressed_texture(const GrSurfaceDesc& desc,
cblume55f2d2d2016-02-26 13:20:48 -0800976 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800977 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800978 GrGLenum target,
979 GrGLenum internalFormat,
980 GrGLenum externalFormat,
981 GrGLenum externalType,
982 const SkTArray<GrMipLevel>& texels,
bsalomone699d0c2016-03-09 06:25:15 -0800983 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800984 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800985
986 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
987 // We can only use TexStorage if we know we will not later change the storage requirements.
988 // This means if we may later want to add mipmaps, we cannot use TexStorage.
989 // Right now, we cannot know if we will later add mipmaps or not.
990 // The only time we can use TexStorage is when we already have the
991 // mipmaps.
992 useTexStorage &= texels.count() > 1;
993
994 if (useTexStorage) {
995 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800996 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -0800997 TexStorage2D(target,
998 texels.count(),
999 internalFormat,
1000 desc.fWidth, desc.fHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001001 GrGLenum error = check_alloc_error(desc, &interface);
1002 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001003 return false;
cblume790d5132016-02-29 11:13:29 -08001004 } else {
1005 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1006 const void* currentMipData = texels[currentMipLevel].fPixels;
1007 if (currentMipData == nullptr) {
1008 continue;
1009 }
1010 int twoToTheMipLevel = 1 << currentMipLevel;
1011 int currentWidth = SkTMax(1, desc.fWidth / twoToTheMipLevel);
1012 int currentHeight = SkTMax(1, desc.fHeight / twoToTheMipLevel);
1013
1014 GR_GL_CALL(&interface,
1015 TexSubImage2D(target,
1016 currentMipLevel,
1017 0, // left
1018 0, // top
1019 currentWidth,
1020 currentHeight,
1021 externalFormat, externalType,
1022 currentMipData));
1023 }
bsalomone699d0c2016-03-09 06:25:15 -08001024 return true;
cblume790d5132016-02-29 11:13:29 -08001025 }
1026 } else {
bsalomone699d0c2016-03-09 06:25:15 -08001027 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -08001028 GL_ALLOC_CALL(&interface,
1029 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001030 0,
cblume790d5132016-02-29 11:13:29 -08001031 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001032 baseWidth,
1033 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001034 0, // border
1035 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001036 nullptr));
cblume790d5132016-02-29 11:13:29 -08001037 GrGLenum error = check_alloc_error(desc, &interface);
1038 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001039 return false;
1040 }
1041 } else {
1042 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1043 int twoToTheMipLevel = 1 << currentMipLevel;
1044 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1045 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1046 const void* currentMipData = texels[currentMipLevel].fPixels;
1047 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1048 // This will allocate texture memory which we can later populate.
1049 GL_ALLOC_CALL(&interface,
1050 TexImage2D(target,
1051 currentMipLevel,
1052 internalFormat,
1053 currentWidth,
1054 currentHeight,
1055 0, // border
1056 externalFormat, externalType,
1057 currentMipData));
1058 GrGLenum error = check_alloc_error(desc, &interface);
1059 if (error != GR_GL_NO_ERROR) {
1060 return false;
1061 }
cblume790d5132016-02-29 11:13:29 -08001062 }
cblume55f2d2d2016-02-26 13:20:48 -08001063 }
1064 }
bsalomone699d0c2016-03-09 06:25:15 -08001065 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001066}
1067
1068/**
1069 * Creates storage space for the texture and fills it with texels.
1070 *
1071 * @param desc The surface descriptor for the texture being created.
1072 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -08001073 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -08001074 * @param internalFormat The data format used for the internal storage of the texture.
1075 * @param texels The texel data of the texture being created.
cblume55f2d2d2016-02-26 13:20:48 -08001076 */
1077static bool allocate_and_populate_compressed_texture(const GrSurfaceDesc& desc,
1078 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -08001079 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -08001080 GrGLenum target, GrGLenum internalFormat,
1081 const SkTArray<GrMipLevel>& texels,
1082 int baseWidth, int baseHeight) {
1083 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001084
cblume790d5132016-02-29 11:13:29 -08001085 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
1086 // We can only use TexStorage if we know we will not later change the storage requirements.
1087 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1088 // Right now, we cannot know if we will later add mipmaps or not.
1089 // The only time we can use TexStorage is when we already have the
1090 // mipmaps.
1091 useTexStorage &= texels.count() > 1;
cblume55f2d2d2016-02-26 13:20:48 -08001092
cblume790d5132016-02-29 11:13:29 -08001093 if (useTexStorage) {
1094 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001095 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -08001096 TexStorage2D(target,
1097 texels.count(),
1098 internalFormat,
1099 baseWidth, baseHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001100 GrGLenum error = check_alloc_error(desc, &interface);
1101 if (error != GR_GL_NO_ERROR) {
1102 return false;
cblume790d5132016-02-29 11:13:29 -08001103 } else {
1104 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1105 const void* currentMipData = texels[currentMipLevel].fPixels;
1106 if (currentMipData == nullptr) {
1107 continue;
1108 }
1109
1110 int twoToTheMipLevel = 1 << currentMipLevel;
1111 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1112 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1113
1114 // Make sure that the width and height that we pass to OpenGL
1115 // is a multiple of the block size.
1116 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1117 currentHeight);
1118 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1119 currentMipLevel,
1120 0, // left
1121 0, // top
1122 currentWidth,
1123 currentHeight,
1124 internalFormat,
1125 SkToInt(dataSize),
1126 currentMipData));
1127 }
1128 }
1129 } else {
1130 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1131 int twoToTheMipLevel = 1 << currentMipLevel;
1132 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1133 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1134
1135 // Make sure that the width and height that we pass to OpenGL
1136 // is a multiple of the block size.
1137 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, baseWidth, baseHeight);
1138
1139 GL_ALLOC_CALL(&interface,
1140 CompressedTexImage2D(target,
1141 currentMipLevel,
1142 internalFormat,
1143 currentWidth,
1144 currentHeight,
1145 0, // border
1146 SkToInt(dataSize),
1147 texels[currentMipLevel].fPixels));
1148
1149 GrGLenum error = check_alloc_error(desc, &interface);
1150 if (error != GR_GL_NO_ERROR) {
1151 return false;
1152 }
cblume55f2d2d2016-02-26 13:20:48 -08001153 }
1154 }
1155
1156 return true;
1157}
1158
1159/**
1160 * After a texture is created, any state which was altered during its creation
1161 * needs to be restored.
1162 *
1163 * @param interface The GL interface to use.
1164 * @param caps The capabilities of the GL device.
1165 * @param restoreGLRowLength Should the row length unpacking be restored?
1166 * @param glFlipY Did GL flip the texture vertically?
1167 */
1168static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1169 bool restoreGLRowLength, bool glFlipY) {
1170 if (restoreGLRowLength) {
1171 SkASSERT(caps.unpackRowLengthSupport());
1172 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1173 }
1174 if (glFlipY) {
1175 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1176 }
1177}
1178
bsalomon861e1032014-12-16 07:33:49 -08001179bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -08001180 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -08001181 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001182 int left, int top, int width, int height,
1183 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001184 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001185 // If we're uploading compressed data then we should be using uploadCompressedTexData
1186 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1187
bsalomon5b30c6f2015-12-17 14:17:34 -08001188 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
1189
cblume55f2d2d2016-02-26 13:20:48 -08001190 // texels is const.
1191 // But we may need to flip the texture vertically to prepare it.
1192 // Rather than flip in place and alter the incoming data,
1193 // we allocate a new buffer to flip into.
1194 // This means we need to make a non-const shallow copy of texels.
1195 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1196
1197 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1198 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -08001199 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001200 }
1201
1202 const GrGLInterface* interface = this->glInterface();
1203 const GrGLCaps& caps = this->glCaps();
1204
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001205 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001206
1207 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001208 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001209 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001210
cblume55f2d2d2016-02-26 13:20:48 -08001211 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1212 int twoToTheMipLevel = 1 << currentMipLevel;
1213 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1214 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1215
cblume55f2d2d2016-02-26 13:20:48 -08001216 if (currentHeight > SK_MaxS32 ||
1217 currentWidth > SK_MaxS32) {
1218 return false;
1219 }
1220 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
1221 &currentWidth,
1222 &currentHeight,
1223 &texelsShallowCopy[currentMipLevel].fPixels,
1224 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
1225 return false;
1226 }
1227 if (currentWidth < 0 || currentHeight < 0) {
1228 return false;
1229 }
1230 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001231
bsalomon5b30c6f2015-12-17 14:17:34 -08001232 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001233 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001234 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001235 GrGLenum externalFormat;
1236 GrGLenum externalType;
1237 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
1238 &externalFormat, &externalType)) {
1239 return false;
1240 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001241 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001242 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001243 * because our srcData has extra bytes past each row. If so, we need
1244 * to trim those off here, since GL ES may not let us specify
1245 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001246 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001247 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001248 bool swFlipY = false;
1249 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001250
bsalomone699d0c2016-03-09 06:25:15 -08001251 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001252 if (caps.unpackFlipYSupport()) {
1253 glFlipY = true;
1254 } else {
1255 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001256 }
cblume55f2d2d2016-02-26 13:20:48 -08001257 }
1258
1259 // in case we need a temporary, trimmed copy of the src pixels
1260 SkAutoSMalloc<128 * 128> tempStorage;
1261
1262 // find the combined size of all the mip levels and the relative offset of
1263 // each into the collective buffer
1264 size_t combined_buffer_size = 0;
1265 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1266 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1267 int twoToTheMipLevel = 1 << currentMipLevel;
1268 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1269 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1270 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1271 individual_mip_offsets.push_back(combined_buffer_size);
1272 combined_buffer_size += trimmedSize;
1273 }
1274 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1275
1276 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001277 int twoToTheMipLevel = 1 << currentMipLevel;
1278 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1279 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1280 const size_t trimRowBytes = currentWidth * bpp;
1281
1282 /*
1283 * check whether to allocate a temporary buffer for flipping y or
1284 * because our srcData has extra bytes past each row. If so, we need
1285 * to trim those off here, since GL ES may not let us specify
1286 * GL_UNPACK_ROW_LENGTH.
1287 */
1288 restoreGLRowLength = false;
1289
1290 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
1291 if (caps.unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001292 // can't use this for flipping, only non-neg values allowed. :(
1293 if (rowBytes != trimRowBytes) {
1294 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001295 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001296 restoreGLRowLength = true;
1297 }
jvanverth17aa0472016-01-05 10:41:27 -08001298 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001299 if (trimRowBytes != rowBytes || swFlipY) {
1300 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001301 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1302 if (swFlipY && currentHeight >= 1) {
1303 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001304 }
cblume55f2d2d2016-02-26 13:20:48 -08001305 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1306 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001307 memcpy(dst, src, trimRowBytes);
1308 if (swFlipY) {
1309 src -= rowBytes;
1310 } else {
1311 src += rowBytes;
1312 }
1313 dst += trimRowBytes;
1314 }
1315 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001316 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1317 individual_mip_offsets[currentMipLevel];
1318 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001319 }
jvanverth17aa0472016-01-05 10:41:27 -08001320 } else {
1321 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001322 }
bsalomone699d0c2016-03-09 06:25:15 -08001323 }
1324
1325 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001326 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001327 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001328 }
cblume55f2d2d2016-02-26 13:20:48 -08001329 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT,
1330 config_alignment(desc.fConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001331 }
cblume55f2d2d2016-02-26 13:20:48 -08001332
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001333 bool succeeded = true;
cblume55f2d2d2016-02-26 13:20:48 -08001334 if (kNewTexture_UploadType == uploadType &&
1335 0 == left && 0 == top &&
1336 desc.fWidth == width && desc.fHeight == height &&
1337 !desc.fTextureStorageAllocator.fAllocateTextureStorage) {
bsalomone699d0c2016-03-09 06:25:15 -08001338 succeeded = allocate_and_populate_uncompressed_texture(desc, *interface, caps, target,
1339 internalFormat, externalFormat,
1340 externalType, texelsShallowCopy,
1341 width, height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001342 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001343 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001344 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001345 }
cblume55f2d2d2016-02-26 13:20:48 -08001346 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1347 currentMipLevel++) {
1348 int twoToTheMipLevel = 1 << currentMipLevel;
1349 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1350 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001351
1352 GL_CALL(TexSubImage2D(target,
1353 currentMipLevel,
1354 left, top,
1355 currentWidth,
1356 currentHeight,
1357 externalFormat, externalType,
1358 texelsShallowCopy[currentMipLevel].fPixels));
1359 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001360 }
1361
cblume55f2d2d2016-02-26 13:20:48 -08001362 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1363
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001364 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001365}
1366
krajcevski145d48c2014-06-11 16:07:50 -07001367// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001368// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001369// create a CompressedTexData struct that takes a desc/ptr and figures out
1370// the proper upload semantics. Then users can construct this function how they
1371// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -08001372bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -07001373 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -08001374 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -08001375 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001376 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -08001377 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
krajcevski9c0e6292014-06-02 07:38:14 -07001378
1379 // No support for software flip y, yet...
1380 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
1381
cblume55f2d2d2016-02-26 13:20:48 -08001382 const GrGLInterface* interface = this->glInterface();
1383 const GrGLCaps& caps = this->glCaps();
1384
krajcevski145d48c2014-06-11 16:07:50 -07001385 if (-1 == width) {
1386 width = desc.fWidth;
1387 }
1388#ifdef SK_DEBUG
1389 else {
1390 SkASSERT(width <= desc.fWidth);
1391 }
1392#endif
1393
1394 if (-1 == height) {
1395 height = desc.fHeight;
1396 }
1397#ifdef SK_DEBUG
1398 else {
1399 SkASSERT(height <= desc.fHeight);
1400 }
1401#endif
1402
bsalomon76148af2016-01-12 11:13:47 -08001403 // We only need the internal format for compressed 2D textures.
1404 GrGLenum internalFormat;
cblume55f2d2d2016-02-26 13:20:48 -08001405 if (!caps.getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001406 return false;
1407 }
krajcevski9c0e6292014-06-02 07:38:14 -07001408
jvanverth17aa0472016-01-05 10:41:27 -08001409 if (kNewTexture_UploadType == uploadType) {
cblume790d5132016-02-29 11:13:29 -08001410 return allocate_and_populate_compressed_texture(desc, *interface, caps, target,
1411 internalFormat, texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001412 } else {
bsalomond4cb9222014-08-11 14:19:09 -07001413 // Paletted textures can't be updated.
1414 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
1415 return false;
1416 }
cblume55f2d2d2016-02-26 13:20:48 -08001417 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001418 SkASSERT(texels[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001419
1420 int twoToTheMipLevel = 1 << currentMipLevel;
1421 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1422 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1423
1424 // Make sure that the width and height that we pass to OpenGL
1425 // is a multiple of the block size.
1426 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1427 currentHeight);
1428 GL_CALL(CompressedTexSubImage2D(target,
1429 currentMipLevel,
1430 left, top,
1431 currentWidth,
1432 currentHeight,
1433 internalFormat,
1434 SkToInt(dataSize),
1435 texels[currentMipLevel].fPixels));
1436 }
krajcevski145d48c2014-06-11 16:07:50 -07001437 }
krajcevski9c0e6292014-06-02 07:38:14 -07001438
bsalomond4cb9222014-08-11 14:19:09 -07001439 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001440}
1441
bsalomon424cc262015-05-22 10:37:30 -07001442static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001443 int sampleCount,
1444 GrGLenum format,
1445 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001446 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001447 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001448 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001449 case GrGLCaps::kDesktop_ARB_MSFBOType:
1450 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001451 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001452 case GrGLCaps::kES_3_0_MSFBOType:
1453 GL_ALLOC_CALL(ctx.interface(),
1454 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1455 sampleCount,
1456 format,
1457 width, height));
1458 break;
1459 case GrGLCaps::kES_Apple_MSFBOType:
1460 GL_ALLOC_CALL(ctx.interface(),
1461 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1462 sampleCount,
1463 format,
1464 width, height));
1465 break;
1466 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1467 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1468 GL_ALLOC_CALL(ctx.interface(),
1469 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1470 sampleCount,
1471 format,
1472 width, height));
1473 break;
1474 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001475 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001476 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001477 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001478 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001479}
1480
egdanielb0e1be22015-04-22 13:27:39 -07001481bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
1482 GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -08001483 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001484 GrGLRenderTarget::IDDesc* idDesc) {
1485 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001486 idDesc->fRTFBOID = 0;
1487 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -07001488 idDesc->fLifeCycle = lifeCycle;
vbuzinovdded6962015-06-12 08:59:45 -07001489 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
1490 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
1491 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001492
1493 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001494
bsalomona11e5fc2015-12-18 07:59:41 -08001495 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001496
bsalomonb15b4c12014-10-29 12:41:57 -07001497 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001498 goto FAILED;
1499 }
1500
egdanield803f272015-03-18 13:01:52 -07001501 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1502 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001503 goto FAILED;
1504 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001505
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001506 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1507 // the texture bound to the other. The exception is the IMG multisample extension. With this
1508 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1509 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001510 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001511 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001512 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001513 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001514 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001515 goto FAILED;
1516 }
bsalomon76148af2016-01-12 11:13:47 -08001517 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1518 return false;
1519 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001520 } else {
egdanield803f272015-03-18 13:01:52 -07001521 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001522 }
1523
egdanield803f272015-03-18 13:01:52 -07001524 // below here we may bind the FBO
1525 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1526 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001527 SkASSERT(desc.fSampleCnt > 0);
1528 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001529 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001530 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001531 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001532 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001533 goto FAILED;
1534 }
egdanield803f272015-03-18 13:01:52 -07001535 fStats.incRenderTargetBinds();
1536 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1537 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001538 GR_GL_COLOR_ATTACHMENT0,
1539 GR_GL_RENDERBUFFER,
1540 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001541 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1542 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001543 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1544 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1545 goto FAILED;
1546 }
bsalomon424cc262015-05-22 10:37:30 -07001547 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001548 }
1549 }
egdanield803f272015-03-18 13:01:52 -07001550 fStats.incRenderTargetBinds();
1551 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552
bsalomonb15b4c12014-10-29 12:41:57 -07001553 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001554 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001555 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001556 texInfo.fTarget,
1557 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001558 } else {
egdanield803f272015-03-18 13:01:52 -07001559 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001560 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001561 texInfo.fTarget,
1562 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001563 }
bsalomonb15b4c12014-10-29 12:41:57 -07001564 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1565 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001566 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001567 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1568 goto FAILED;
1569 }
bsalomon424cc262015-05-22 10:37:30 -07001570 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001571 }
1572
1573 return true;
1574
1575FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001576 if (idDesc->fMSColorRenderbufferID) {
1577 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001578 }
egdanield803f272015-03-18 13:01:52 -07001579 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1580 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001581 }
egdanield803f272015-03-18 13:01:52 -07001582 if (idDesc->fTexFBOID) {
1583 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001584 }
1585 return false;
1586}
1587
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001588// good to set a break-point here to know when createTexture fails
1589static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001590// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001591 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001592}
1593
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001594#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001595static size_t as_size_t(int x) {
1596 return x;
1597}
1598#endif
1599
cblume55f2d2d2016-02-26 13:20:48 -08001600static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface,
1601 GrGpuResource::LifeCycle lifeCycle) {
1602 GrGLTexture::IDDesc idDesc;
1603 idDesc.fInfo.fID = 0;
1604 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
1605 idDesc.fLifeCycle = lifeCycle;
1606 // When we create the texture, we only
1607 // create GL_TEXTURE_2D at the moment.
1608 // External clients can do something different.
1609 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1610 return idDesc;
1611}
1612
1613static void set_initial_texture_params(const GrGLInterface* interface,
1614 const GrGLTextureInfo& info,
1615 GrGLTexture::TexParams* initialTexParams) {
1616 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1617 // drivers have a bug where an FBO won't be complete if it includes a
1618 // texture that is not mipmap complete (considering the filter in use).
1619 // we only set a subset here so invalidate first
1620 initialTexParams->invalidate();
1621 initialTexParams->fMinFilter = GR_GL_NEAREST;
1622 initialTexParams->fMagFilter = GR_GL_NEAREST;
1623 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1624 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1625 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1626 GR_GL_TEXTURE_MAG_FILTER,
1627 initialTexParams->fMagFilter));
1628 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1629 GR_GL_TEXTURE_MIN_FILTER,
1630 initialTexParams->fMinFilter));
1631 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1632 GR_GL_TEXTURE_WRAP_S,
1633 initialTexParams->fWrapS));
1634 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1635 GR_GL_TEXTURE_WRAP_T,
1636 initialTexParams->fWrapT));
1637}
1638
egdanielb0e1be22015-04-22 13:27:39 -07001639GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1640 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -08001641 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001642 // We fail if the MSAA was requested and is not available.
1643 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001644 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001645 return return_null_texture();
1646 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001647
bsalomonf2703d82014-10-28 14:33:06 -07001648 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001649
bsalomonb15b4c12014-10-29 12:41:57 -07001650 GrGLTexture::IDDesc idDesc;
egdanielb0e1be22015-04-22 13:27:39 -07001651 idDesc.fLifeCycle = lifeCycle;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001652 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001653 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001654 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001655 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001656
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001657 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001659 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001660 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001661 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001662
bsalomon091f60c2015-11-10 11:54:56 -08001663 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fInfo, &rtIDDesc)) {
1664 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001665 return return_null_texture();
1666 }
halcanary385fe4d2015-08-26 13:07:48 -07001667 tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001668 } else {
cblume55f2d2d2016-02-26 13:20:48 -08001669 bool wasMipMapDataProvided = false;
1670 if (texels.count() > 1) {
1671 wasMipMapDataProvided = true;
1672 }
1673 tex = new GrGLTexture(this, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001674 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001675 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001676#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001677 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
cblume55f2d2d2016-02-26 13:20:48 -08001678 glTexDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001679#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001680 return tex;
1681}
1682
egdanielb0e1be22015-04-22 13:27:39 -07001683GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1684 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -08001685 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001686 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001687 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001688 return return_null_texture();
1689 }
1690
cblume55f2d2d2016-02-26 13:20:48 -08001691 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface(), lifeCycle);
bsalomon091f60c2015-11-10 11:54:56 -08001692 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001693 return return_null_texture();
1694 }
1695
1696 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001697 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001698
krajcevski9c0e6292014-06-02 07:38:14 -07001699 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001700 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001701
cblume55f2d2d2016-02-26 13:20:48 -08001702 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001703 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001704 return return_null_texture();
1705 }
1706
1707 GrGLTexture* tex;
halcanary385fe4d2015-08-26 13:07:48 -07001708 tex = new GrGLTexture(this, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001709 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1710#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001711 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
cblume55f2d2d2016-02-26 13:20:48 -08001712 glTexDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001713#endif
1714 return tex;
1715}
1716
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001717namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001718
egdaniel8dc7c3a2015-04-16 11:22:42 -07001719const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001720
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001721void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001722 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001723
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001724 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001725 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001726 (kUnknownBitCount == format->fTotalBits));
1727 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001728 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001729 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1730 (GrGLint*)&format->fStencilBits);
1731 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001732 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001733 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1734 (GrGLint*)&format->fTotalBits);
1735 format->fTotalBits += format->fStencilBits;
1736 } else {
1737 format->fTotalBits = format->fStencilBits;
1738 }
1739 }
1740}
1741}
1742
egdanielff1d5472015-09-10 08:37:20 -07001743int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001744 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001745 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001746 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1747 // Default to unsupported, set this if we find a stencil format that works.
1748 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001749 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001750 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001751 GL_CALL(GenTextures(1, &colorID));
1752 this->setScratchTextureUnit();
1753 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1754 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1755 GR_GL_TEXTURE_MAG_FILTER,
1756 GR_GL_NEAREST));
1757 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1758 GR_GL_TEXTURE_MIN_FILTER,
1759 GR_GL_NEAREST));
1760 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1761 GR_GL_TEXTURE_WRAP_S,
1762 GR_GL_CLAMP_TO_EDGE));
1763 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1764 GR_GL_TEXTURE_WRAP_T,
1765 GR_GL_CLAMP_TO_EDGE));
1766
bsalomon76148af2016-01-12 11:13:47 -08001767 GrGLenum internalFormat;
1768 GrGLenum externalFormat;
1769 GrGLenum externalType;
1770 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1771 &externalType)) {
1772 return false;
1773 }
egdanielff1d5472015-09-10 08:37:20 -07001774 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1775 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001776 0,
bsalomon76148af2016-01-12 11:13:47 -08001777 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001778 kSize,
1779 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001780 0,
bsalomon76148af2016-01-12 11:13:47 -08001781 externalFormat,
1782 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001783 NULL));
bsalomon30447372015-12-21 09:03:05 -08001784 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001785 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001786 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001787 }
1788
1789 // unbind the texture from the texture unit before binding it to the frame buffer
1790 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1791
1792 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001793 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001794 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001795 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001796 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1797 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1798 GR_GL_COLOR_ATTACHMENT0,
1799 GR_GL_TEXTURE_2D,
1800 colorID,
1801 0));
bsalomon30447372015-12-21 09:03:05 -08001802 GrGLuint sbRBID = 0;
1803 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001804
1805 // look over formats till I find a compatible one
1806 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001807 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001808 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001809 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1810 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1811 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1812 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1813 sFmt.fInternalFormat,
1814 kSize, kSize));
1815 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001816 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001817 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001818 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001819 if (sFmt.fPacked) {
1820 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1821 GR_GL_DEPTH_ATTACHMENT,
1822 GR_GL_RENDERBUFFER, sbRBID));
1823 } else {
1824 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1825 GR_GL_DEPTH_ATTACHMENT,
1826 GR_GL_RENDERBUFFER, 0));
1827 }
1828 GrGLenum status;
1829 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1830 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1831 firstWorkingStencilFormatIndex = i;
1832 break;
1833 }
egdanielff1d5472015-09-10 08:37:20 -07001834 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1835 GR_GL_STENCIL_ATTACHMENT,
1836 GR_GL_RENDERBUFFER, 0));
1837 if (sFmt.fPacked) {
1838 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1839 GR_GL_DEPTH_ATTACHMENT,
1840 GR_GL_RENDERBUFFER, 0));
1841 }
egdanielff1d5472015-09-10 08:37:20 -07001842 }
1843 }
bsalomon30447372015-12-21 09:03:05 -08001844 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001845 }
1846 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001847 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1848 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001849 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001850 }
bsalomon30447372015-12-21 09:03:05 -08001851 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001852}
1853
erikchen9a1ed5d2016-02-10 16:32:34 -08001854bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001855 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1856 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001857 if (desc.fTextureStorageAllocator.fAllocateTextureStorage) {
cblume55f2d2d2016-02-26 13:20:48 -08001858 return this->createTextureExternalAllocatorImpl(desc, info, texels);
erikchen9a1ed5d2016-02-10 16:32:34 -08001859 }
1860
1861 info->fID = 0;
1862 info->fTarget = GR_GL_TEXTURE_2D;
1863 GL_CALL(GenTextures(1, &(info->fID)));
1864
1865 if (!info->fID) {
1866 return false;
1867 }
1868
1869 this->setScratchTextureUnit();
1870 GL_CALL(BindTexture(info->fTarget, info->fID));
1871
1872 if (renderTarget && this->glCaps().textureUsageSupport()) {
1873 // provides a hint about how this texture will be used
1874 GL_CALL(TexParameteri(info->fTarget,
1875 GR_GL_TEXTURE_USAGE,
1876 GR_GL_FRAMEBUFFER_ATTACHMENT));
1877 }
1878
cblume55f2d2d2016-02-26 13:20:48 -08001879 if (info) {
1880 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1881 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001882 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1883 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001884 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001885 GL_CALL(DeleteTextures(1, &(info->fID)));
1886 return false;
1887 }
1888 return true;
1889}
1890
cblume55f2d2d2016-02-26 13:20:48 -08001891bool GrGLGpu::createTextureExternalAllocatorImpl(const GrSurfaceDesc& desc,
1892 GrGLTextureInfo* info,
1893 const SkTArray<GrMipLevel>& texels) {
1894 // We do not make SkTArray available outside of Skia,
1895 // and so we do not want to allow mipmaps to external
1896 // allocators just yet.
bsalomone699d0c2016-03-09 06:25:15 -08001897 SkASSERT(texels.count() < 2);
cblume55f2d2d2016-02-26 13:20:48 -08001898
bsalomone699d0c2016-03-09 06:25:15 -08001899 const void* pixels = nullptr;
1900 if (!texels.empty()) {
1901 pixels = texels.begin()->fPixels;
1902 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001903 switch (desc.fTextureStorageAllocator.fAllocateTextureStorage(
1904 desc.fTextureStorageAllocator.fCtx, reinterpret_cast<GrBackendObject>(info),
bsalomone699d0c2016-03-09 06:25:15 -08001905 desc.fWidth, desc.fHeight, desc.fConfig, pixels, desc.fOrigin)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001906 case GrTextureStorageAllocator::Result::kSucceededAndUploaded:
1907 return true;
1908 case GrTextureStorageAllocator::Result::kFailed:
1909 return false;
1910 case GrTextureStorageAllocator::Result::kSucceededWithoutUpload:
1911 break;
1912 }
1913
1914 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1915 desc.fWidth, desc.fHeight,
bsalomone699d0c2016-03-09 06:25:15 -08001916 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001917 desc.fTextureStorageAllocator.fDeallocateTextureStorage(
1918 desc.fTextureStorageAllocator.fCtx, reinterpret_cast<GrBackendObject>(info));
1919 return false;
1920 }
1921 return true;
1922}
1923
egdanielec00d942015-09-14 12:56:10 -07001924GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1925 int width,
1926 int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001927 SkASSERT(width >= rt->width());
1928 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001929
vbuzinovdded6962015-06-12 08:59:45 -07001930 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001931 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001932
egdanielff1d5472015-09-10 08:37:20 -07001933 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001934 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001935 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001936 }
egdanielff1d5472015-09-10 08:37:20 -07001937
1938 if (!sbDesc.fRenderbufferID) {
1939 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1940 }
1941 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001942 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001943 }
1944 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1945 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1946 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1947 // we do this "if" so that we don't call the multisample
1948 // version on a GL that doesn't have an MSAA extension.
1949 if (samples > 0) {
1950 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1951 samples,
1952 sFmt.fInternalFormat,
1953 width, height));
1954 } else {
1955 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1956 sFmt.fInternalFormat,
1957 width, height));
1958 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1959 }
1960 fStats.incStencilAttachmentCreates();
1961 // After sized formats we attempt an unsized format and take
1962 // whatever sizes GL gives us. In that case we query for the size.
1963 GrGLStencilAttachment::Format format = sFmt;
1964 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001965 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1966 sbDesc,
1967 width,
1968 height,
1969 samples,
1970 format);
1971 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001972}
1973
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001974////////////////////////////////////////////////////////////////////////////////
1975
jvanverth73063dc2015-12-03 09:15:47 -08001976// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1977// objects are implemented as client-side-arrays on tile-deferred architectures.
1978#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1979
cdaltone2e71c22016-04-07 18:13:29 -07001980GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
1981 GrAccessPattern accessPattern) {
1982 return GrGLBuffer::Create(this, size, intendedType, accessPattern);
jvanverth73063dc2015-12-03 09:15:47 -08001983}
1984
bsalomon3e791242014-12-17 13:43:13 -08001985void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001986 const GrGLIRect& rtViewport,
1987 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001988 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001989 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001990 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001991 scissorState.rect().fLeft,
1992 scissorState.rect().fTop,
1993 scissorState.rect().width(),
1994 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001995 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001996 // if the scissor fully contains the viewport then we fall through and
1997 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001998 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001999 if (fHWScissorSettings.fRect != scissor) {
2000 scissor.pushToGLScissor(this->glInterface());
2001 fHWScissorSettings.fRect = scissor;
2002 }
2003 if (kYes_TriState != fHWScissorSettings.fEnabled) {
2004 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2005 fHWScissorSettings.fEnabled = kYes_TriState;
2006 }
2007 return;
2008 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002009 }
joshualitt77b13072014-10-27 14:51:01 -07002010
2011 // See fall through note above
2012 this->disableScissor();
2013}
2014
ethannicholas28ef4452016-03-25 09:26:03 -07002015void GrGLGpu::flushMinSampleShading(float minSampleShading) {
2016 if (fHWMinSampleShading != minSampleShading) {
2017 if (minSampleShading > 0.0) {
2018 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
2019 GL_CALL(MinSampleShading(minSampleShading));
2020 }
2021 else {
2022 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
2023 }
2024 fHWMinSampleShading = minSampleShading;
2025 }
2026}
2027
egdaniel0e1853c2016-03-17 11:35:45 -07002028bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc) {
egdaniel080e6732014-12-22 07:35:52 -08002029 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002030 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002031
egdaniel080e6732014-12-22 07:35:52 -08002032 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08002033 this->flushDrawFace(pipeline.getDrawFace());
ethannicholas28ef4452016-03-25 09:26:03 -07002034 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08002035
egdaniel0e1853c2016-03-17 11:35:45 -07002036 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc));
bsalomon6df86402015-06-01 10:41:49 -07002037 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07002038 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08002039 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08002040 }
2041
bsalomon6df86402015-06-01 10:41:49 -07002042 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08002043 if (fHWProgramID != programID) {
2044 GL_CALL(UseProgram(programID));
2045 fHWProgramID = programID;
2046 }
2047
egdanield803f272015-03-18 13:01:52 -07002048 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08002049 // Swizzle the blend to match what the shader will output.
2050 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07002051 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08002052 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07002053 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002054
cdalton42717652015-06-18 11:54:30 -07002055 SkSTArray<8, const GrTextureAccess*> textureAccesses;
egdaniel0e1853c2016-03-17 11:35:45 -07002056 program->setData(primProc, pipeline, &textureAccesses);
cdalton42717652015-06-18 11:54:30 -07002057
2058 int numTextureAccesses = textureAccesses.count();
2059 for (int i = 0; i < numTextureAccesses; i++) {
brianosman898235c2016-04-06 07:38:23 -07002060 this->bindTexture(i, textureAccesses[i]->getParams(), pipeline.getAllowSRGBInputs(),
cdalton42717652015-06-18 11:54:30 -07002061 static_cast<GrGLTexture*>(textureAccesses[i]->getTexture()));
2062 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002063
brianosman898235c2016-04-06 07:38:23 -07002064 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -08002065 this->flushStencil(pipeline.getStencil());
2066 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltonaf8bc7d2016-02-05 09:35:20 -08002067 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !pipeline.getStencil().isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08002068
2069 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002070 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07002071 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08002072
2073 return true;
2074}
2075
joshualitt873ad0e2015-01-20 09:08:51 -08002076void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
egdaniel0e1853c2016-03-17 11:35:45 -07002077 const GrNonInstancedMesh& mesh,
bsalomonbc3d0de2014-12-15 13:45:03 -08002078 size_t* indexOffsetInBytes) {
cdalton397536c2016-03-25 12:15:03 -07002079 const GrGLBuffer* vbuf;
2080 vbuf = static_cast<const GrGLBuffer*>(mesh.vertexBuffer());
bsalomonbc3d0de2014-12-15 13:45:03 -08002081
2082 SkASSERT(vbuf);
2083 SkASSERT(!vbuf->isMapped());
2084
cdaltone2e71c22016-04-07 18:13:29 -07002085 GrGLAttribArrayState* attribState;
egdaniel0e1853c2016-03-17 11:35:45 -07002086 if (mesh.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002087 SkASSERT(indexOffsetInBytes);
2088
2089 *indexOffsetInBytes = 0;
cdaltone2e71c22016-04-07 18:13:29 -07002090 const GrGLBuffer* ibuf = static_cast<const GrGLBuffer*>(mesh.indexBuffer());
bsalomonbc3d0de2014-12-15 13:45:03 -08002091
2092 SkASSERT(ibuf);
2093 SkASSERT(!ibuf->isMapped());
2094 *indexOffsetInBytes += ibuf->baseOffset();
cdaltone2e71c22016-04-07 18:13:29 -07002095 attribState = fHWVertexArrayState.bindInternalVertexArray(this, ibuf);
2096 } else {
2097 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002098 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002099
joshualitt873ad0e2015-01-20 09:08:51 -08002100 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08002101 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002102
joshualitt873ad0e2015-01-20 09:08:51 -08002103 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08002104
egdaniel0e1853c2016-03-17 11:35:45 -07002105 size_t vertexOffsetInBytes = stride * mesh.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08002106
2107 vertexOffsetInBytes += vbuf->baseOffset();
2108
bsalomonbc3d0de2014-12-15 13:45:03 -08002109 uint32_t usedAttribArraysMask = 0;
2110 size_t offset = 0;
2111
2112 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08002113 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08002114 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08002115 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08002116 attribState->set(this,
2117 attribIndex,
cdaltone2e71c22016-04-07 18:13:29 -07002118 vbuf,
cdalton793dc262016-02-08 10:11:47 -08002119 attribType,
bsalomonbc3d0de2014-12-15 13:45:03 -08002120 stride,
2121 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08002122 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002123 }
2124 attribState->disableUnusedArrays(this, usedAttribArraysMask);
2125 }
2126}
2127
cdaltone2e71c22016-04-07 18:13:29 -07002128GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrGLBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002129 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002130
cdaltone2e71c22016-04-07 18:13:29 -07002131 // Index buffer state is tied to the vertex array.
2132 if (kIndex_GrBufferType == type) {
2133 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002134 }
cdaltone2e71c22016-04-07 18:13:29 -07002135
2136 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2137 auto& bufferState = fHWBufferState[type];
2138
2139 if (buffer->getUniqueID() != bufferState.fBoundBufferUniqueID) {
2140 if (!buffer->isCPUBacked() || !bufferState.fBufferZeroKnownBound) {
2141 GL_CALL(BindBuffer(bufferState.fGLTarget, buffer->bufferID()));
2142 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
2143 }
2144 bufferState.fBoundBufferUniqueID = buffer->getUniqueID();
2145 }
2146
2147 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002148}
2149
bsalomon861e1032014-12-16 07:33:49 -08002150void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002151 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002152 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002153 fHWScissorSettings.fEnabled = kNo_TriState;
2154 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002155 }
2156}
2157
egdaniel51c8d402015-08-06 10:54:13 -07002158void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002159 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002160 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002161 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002162
egdaniel51c8d402015-08-06 10:54:13 -07002163 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08002164 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07002165 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002166 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00002167
2168 GrGLfloat r, g, b, a;
2169 static const GrGLfloat scale255 = 1.f / 255.f;
2170 a = GrColorUnpackA(color) * scale255;
2171 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002172 r = GrColorUnpackR(color) * scaleRGB;
2173 g = GrColorUnpackG(color) * scaleRGB;
2174 b = GrColorUnpackB(color) * scaleRGB;
2175
2176 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002177 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002178 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002179 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002180}
2181
bsalomon861e1032014-12-16 07:33:49 -08002182void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08002183 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002184 if (!this->caps()->discardRenderTargetSupport()) {
2185 return;
2186 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002187
2188 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002189 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
2190 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
2191 fStats.incRenderTargetBinds();
2192 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
2193 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002194 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07002195 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002196 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002197 break;
2198 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07002199 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002200 // When rendering to the default framebuffer the legal values for attachments
2201 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2202 // types.
2203 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002204 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002205 attachments));
2206 } else {
2207 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002208 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002209 attachments));
2210 }
2211 break;
2212 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07002213 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002214 // When rendering to the default framebuffer the legal values for attachments
2215 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2216 // types. See glDiscardFramebuffer() spec.
2217 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002218 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002219 attachments));
2220 } else {
2221 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002222 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002223 attachments));
2224 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002225 break;
2226 }
2227 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002228 renderTarget->flagAsResolved();
2229}
2230
bsalomon861e1032014-12-16 07:33:49 -08002231void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002232 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002233 return;
2234 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002235 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002236 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002237
joshualitt77b13072014-10-27 14:51:01 -07002238 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002239
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002240 GL_CALL(StencilMask(0xffffffff));
2241 GL_CALL(ClearStencil(0));
2242 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002243 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002244}
2245
bsalomon861e1032014-12-16 07:33:49 -08002246void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07002247 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002248
egdaniel8dc7c3a2015-04-16 11:22:42 -07002249 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002250 // this should only be called internally when we know we have a
2251 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002252 SkASSERT(sb);
2253 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002254#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002255 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002256 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002257#else
2258 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002259 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002260 // turned into draws. Our contract on GrDrawTarget says that
2261 // changing the clip between stencil passes may or may not
2262 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002263 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002264#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002265 GrGLint value;
2266 if (insideClip) {
2267 value = (1 << (stencilBitCount - 1));
2268 } else {
2269 value = 0;
2270 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002271 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002272 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002273
bsalomon3e791242014-12-17 13:43:13 -08002274 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002275 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002276 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002277
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002278 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002279 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002280 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002281 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002282}
2283
bsalomon39826022015-07-23 08:07:21 -07002284static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
2285 int width, int height, GrPixelConfig config,
2286 size_t rowBytes) {
2287 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002288 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
2289 return false;
2290 }
2291
bsalomon494aa592015-07-23 11:45:02 -07002292 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002293 static const int kMinSize = 32;
2294 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002295 return false;
2296 }
2297
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002298 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002299 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002300 return false;
2301 }
2302
2303 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002304 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002305 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2306 // clipped.
2307 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2308}
2309
bsalomon1aa20292016-01-22 08:16:09 -08002310bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
2311 auto bindRenderTarget = [this, target]() -> bool {
2312 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2313 return true;
2314 };
2315 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2316 GR_GL_GetIntegerv(this->glInterface(), query, value);
2317 };
2318 GrPixelConfig rtConfig = target->config();
2319 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2320}
2321
2322bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
2323 auto bindRenderTarget = [this, rtConfig]() -> bool {
2324 GrTextureDesc desc;
2325 desc.fConfig = rtConfig;
2326 desc.fWidth = desc.fHeight = 16;
2327 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomone699d0c2016-03-09 06:25:15 -08002328 SkAutoTUnref<GrTexture> temp(this->createTexture(desc,
2329 SkBudgeted::kNo));
bsalomon1aa20292016-01-22 08:16:09 -08002330 if (!temp) {
2331 return false;
2332 }
2333 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2334 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2335 return true;
2336 };
2337 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2338 GR_GL_GetIntegerv(this->glInterface(), query, value);
2339 };
2340 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2341}
2342
2343bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2344 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2345 return this->readPixelsSupported(rt, readConfig);
2346 } else {
2347 GrPixelConfig config = surfaceForConfig->config();
2348 return this->readPixelsSupported(config, readConfig);
2349 }
2350}
2351
bsalomone9573312016-01-25 14:33:25 -08002352static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2353 if (GrPixelConfigIsSRGB(a)) {
2354 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2355 } else if (GrPixelConfigIsSRGB(b)) {
2356 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2357 }
2358 return false;
2359}
2360
bsalomonf0674512015-07-28 13:26:15 -07002361bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2362 GrPixelConfig readConfig, DrawPreference* drawPreference,
2363 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002364 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002365
bsalomone9573312016-01-25 14:33:25 -08002366 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002367 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2368 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2369 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2370 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2371 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon100b8f82015-10-28 08:37:44 -07002372 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
bsalomon39826022015-07-23 08:07:21 -07002373
bsalomone9573312016-01-25 14:33:25 -08002374 // For now assume no swizzling, we may change that below.
2375 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2376
2377 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2378 // from will be srcConfig and we will read readConfig pixels from it.
2379 // Not that if we require a draw and return a non-renderable format for the temp surface the
2380 // base class will fail for us.
2381 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2382 tempDrawInfo->fReadConfig = readConfig;
2383
2384 if (requires_srgb_conversion(srcConfig, readConfig)) {
2385 if (!this->readPixelsSupported(readConfig, readConfig)) {
2386 return false;
2387 }
2388 // Draw to do srgb to linear conversion or vice versa.
2389 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2390 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2391 tempDrawInfo->fReadConfig = readConfig;
2392 return true;
2393 }
2394
2395 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
2396 if (!srcAsRT) {
2397 // For now keep assuming the draw is not a format transformation, just a draw to get to a
2398 // RT. We may add additional transformations below.
2399 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2400 }
bsalomon1aa20292016-01-22 08:16:09 -08002401 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2402 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002403 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002404 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2405 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002406 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002407 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002408 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002409 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2410 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002411 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002412 // Better to do a draw with a R/B swap and then read as the original config.
2413 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002414 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2415 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002416 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002417 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2418 if (readConfig == kBGRA_8888_GrPixelConfig &&
2419 this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
2420 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002421 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2422 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2423 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002424 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2425 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2426 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2427 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002428 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
2429 this->glCaps().isConfigRenderable(kSRGBA_8888_GrPixelConfig, false) &&
2430 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2431 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2432 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2433 // will effectively be sBGRA).
2434 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2435 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2436 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2437 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002438 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2439 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2440 // it's unsupported, but 32bit RGBA reads are supported.
2441 // Don't attempt to do any srgb conversions since we only care about alpha.
2442 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2443 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2444 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2445 }
2446 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2447 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2448 // first and then onReadPixels will read that to a 32bit temporary buffer.
2449 if (this->caps()->isConfigRenderable(cpuTempConfig, false)) {
2450 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2451 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2452 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2453 } else {
2454 return false;
2455 }
2456 } else {
2457 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2458 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2459 }
bsalomone6d665e2016-03-10 07:22:25 -08002460 } else if (this->caps()->isConfigRenderable(readConfig, false) &&
2461 this->readPixelsSupported(readConfig, readConfig)) {
2462 // Do a draw to convert from the src config to the read config.
2463 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2464 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2465 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002466 } else {
2467 return false;
2468 }
bsalomon39826022015-07-23 08:07:21 -07002469 }
2470
bsalomon1aa20292016-01-22 08:16:09 -08002471 if (srcAsRT &&
2472 read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002473 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002474 }
2475
bsalomon39826022015-07-23 08:07:21 -07002476 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002477}
2478
bsalomon6cb3cbe2015-07-30 07:34:27 -07002479bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002480 int left, int top,
2481 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002482 GrPixelConfig config,
2483 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002484 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002485 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002486
bsalomone9573312016-01-25 14:33:25 -08002487 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2488 if (!renderTarget) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002489 return false;
2490 }
2491
bsalomon16921ec2015-07-30 15:34:56 -07002492 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002493 if (requires_srgb_conversion(surface->config(), config)) {
2494 return false;
2495 }
2496
2497 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2498 // bit channels as RGBA and then extract A.
2499 if (!this->readPixelsSupported(renderTarget, config)) {
2500 // Don't attempt to do any srgb conversions since we only care about alpha.
2501 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
2502 if (GrPixelConfigIsSRGB(renderTarget->config())) {
2503 tempConfig = kSRGBA_8888_GrPixelConfig;
2504 }
2505 if (kAlpha_8_GrPixelConfig == config &&
2506 this->readPixelsSupported(renderTarget, tempConfig)) {
2507 SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
2508 if (this->onReadPixels(renderTarget, left, top, width, height, tempConfig, temp.get(),
2509 width*4)) {
2510 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2511 for (int j = 0; j < height; ++j) {
2512 for (int i = 0; i < width; ++i) {
2513 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2514 }
2515 }
2516 return true;
2517 }
2518 }
bsalomon16921ec2015-07-30 15:34:56 -07002519 return false;
2520 }
2521
bsalomon76148af2016-01-12 11:13:47 -08002522 GrGLenum externalFormat;
2523 GrGLenum externalType;
bsalomone9573312016-01-25 14:33:25 -08002524 if (!this->glCaps().getReadPixelsFormat(renderTarget->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002525 &externalType)) {
2526 return false;
2527 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002528 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002529
bsalomon@google.comc6980972011-11-02 19:57:21 +00002530 // resolve the render target if necessary
bsalomone9573312016-01-25 14:33:25 -08002531 switch (renderTarget->getResolveType()) {
egdanield803f272015-03-18 13:01:52 -07002532 case GrGLRenderTarget::kCantResolve_ResolveType:
2533 return false;
2534 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002535 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07002536 break;
2537 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002538 this->onResolveRenderTarget(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002539 // we don't track the state of the READ FBO ID.
2540 fStats.incRenderTargetBinds();
bsalomone9573312016-01-25 14:33:25 -08002541 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
egdanield803f272015-03-18 13:01:52 -07002542 break;
2543 default:
2544 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00002545 }
2546
bsalomone9573312016-01-25 14:33:25 -08002547 const GrGLIRect& glvp = renderTarget->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002548
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002549 // the read rect is viewport-relative
2550 GrGLIRect readRect;
bsalomone9573312016-01-25 14:33:25 -08002551 readRect.setRelativeTo(glvp, left, top, width, height, renderTarget->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002552
bsalomon9d02b262016-02-01 12:49:30 -08002553 size_t bytesPerPixel = GrBytesPerPixel(config);
2554 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002555
bsalomon@google.comc6980972011-11-02 19:57:21 +00002556 size_t readDstRowBytes = tightRowBytes;
2557 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002558
bsalomon@google.comc6980972011-11-02 19:57:21 +00002559 // determine if GL can read using the passed rowBytes or if we need
2560 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002561 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002562 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002563 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002564 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002565 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002566 readDstRowBytes = rowBytes;
2567 } else {
2568 scratch.reset(tightRowBytes * height);
2569 readDst = scratch.get();
2570 }
2571 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002572 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002573 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2574 }
bsalomonf46a1242015-12-15 12:37:38 -08002575 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2576
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002577 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2578 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002579 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002580 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002581 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002582 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2583 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002584 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002585 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002586 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002587 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002588
2589 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002590 // API presents top-to-bottom. We must preserve the padding contents. Note
2591 // that the above readPixels did not overwrite the padding.
2592 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002593 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002594 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002595 scratch.reset(tightRowBytes);
2596 void* tmpRow = scratch.get();
2597 // flip y in-place by rows
2598 const int halfY = height >> 1;
2599 char* top = reinterpret_cast<char*>(buffer);
2600 char* bottom = top + (height - 1) * rowBytes;
2601 for (int y = 0; y < halfY; y++) {
2602 memcpy(tmpRow, top, tightRowBytes);
2603 memcpy(top, bottom, tightRowBytes);
2604 memcpy(bottom, tmpRow, tightRowBytes);
2605 top += rowBytes;
2606 bottom -= rowBytes;
2607 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002608 }
2609 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002610 SkASSERT(readDst != buffer);
2611 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002612 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002613 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002614 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002615 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002616 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002617 dst += (height-1) * rowBytes;
2618 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002619 for (int y = 0; y < height; y++) {
2620 memcpy(dst, src, tightRowBytes);
2621 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002622 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002623 dst += rowBytes;
2624 } else {
2625 dst -= rowBytes;
2626 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002627 }
2628 }
2629 return true;
2630}
2631
jvanverthd2d2eb92016-02-17 14:04:46 -08002632void GrGLGpu::finishDrawTarget() {
ethannicholas22793252016-01-30 09:59:10 -08002633 if (fPLSHasBeenUsed) {
halcanary9d524f22016-03-29 09:03:52 -07002634 /* There is an ARM driver bug where if we use PLS, and then draw a frame which does not
2635 * use PLS, it leaves garbage all over the place. As a workaround, we use PLS in a
ethannicholas22793252016-01-30 09:59:10 -08002636 * trivial way every frame. And since we use it every frame, there's never a point at which
2637 * it becomes safe to stop using this workaround once we start.
2638 */
2639 this->disableScissor();
2640 // using PLS in the presence of MSAA results in GL_INVALID_OPERATION
cdaltonaf8bc7d2016-02-05 09:35:20 -08002641 this->flushHWAAState(nullptr, false, false);
ethannicholas22793252016-01-30 09:59:10 -08002642 SkASSERT(!fHWPLSEnabled);
2643 SkASSERT(fMSAAEnabled != kYes_TriState);
2644 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
cdaltone2e71c22016-04-07 18:13:29 -07002645 this->stampPLSSetupRect(SkRect::MakeXYWH(-100.0f, -100.0f, 0.01f, 0.01f));
ethannicholas22793252016-01-30 09:59:10 -08002646 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2647 }
2648}
ethannicholas5366a092016-01-22 09:45:47 -08002649
brianosman64d094d2016-03-25 06:01:59 -07002650void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002651 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002652
egdanield803f272015-03-18 13:01:52 -07002653 uint32_t rtID = target->getUniqueID();
2654 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002655 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002656 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2657#ifdef SK_DEBUG
2658 // don't do this check in Chromium -- this is causing
2659 // lots of repeated command buffer flushes when the compositor is
2660 // rendering with Ganesh, which is really slow; even too slow for
2661 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002662 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002663 GrGLenum status;
2664 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2665 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2666 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2667 }
bsalomon160f24c2015-03-17 15:55:42 -07002668 }
egdanield803f272015-03-18 13:01:52 -07002669#endif
2670 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002671 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002672 }
2673
2674 if (this->glCaps().srgbSupport()) {
2675 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config()) && !disableSRGB;
2676 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2677 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2678 fHWSRGBFramebuffer = kYes_TriState;
2679 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2680 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2681 fHWSRGBFramebuffer = kNo_TriState;
bsalomon16921ec2015-07-30 15:34:56 -07002682 }
bsalomon5cd020f2015-03-17 12:46:56 -07002683 }
brianosman64d094d2016-03-25 06:01:59 -07002684
bsalomon083617b2016-02-12 12:10:14 -08002685 this->didWriteToSurface(target, bounds);
2686}
bsalomona9909122016-01-23 10:41:40 -08002687
bsalomon083617b2016-02-12 12:10:14 -08002688void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2689 if (fHWViewport != viewport) {
2690 viewport.pushToGLViewport(this->glInterface());
2691 fHWViewport = viewport;
2692 }
2693}
2694
2695void GrGLGpu::didWriteToSurface(GrSurface* surface, const SkIRect* bounds) const {
2696 SkASSERT(surface);
bsalomona9909122016-01-23 10:41:40 -08002697 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
bsalomon083617b2016-02-12 12:10:14 -08002698 if (nullptr == bounds || !bounds->isEmpty()) {
2699 if (GrRenderTarget* target = surface->asRenderTarget()) {
2700 target->flagAsNeedingResolve(bounds);
2701 }
2702 if (GrTexture* texture = surface->asTexture()) {
bsalomona9909122016-01-23 10:41:40 -08002703 texture->texturePriv().dirtyMipMaps(true);
2704 }
egdanield803f272015-03-18 13:01:52 -07002705 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002706}
2707
twiz@google.com0f31ca72011-03-18 17:38:11 +00002708GrGLenum gPrimitiveType2GLMode[] = {
2709 GR_GL_TRIANGLES,
2710 GR_GL_TRIANGLE_STRIP,
2711 GR_GL_TRIANGLE_FAN,
2712 GR_GL_POINTS,
2713 GR_GL_LINES,
2714 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002715};
2716
bsalomon@google.comd302f142011-03-03 13:54:13 +00002717#define SWAP_PER_DRAW 0
2718
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002719#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002720 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002721 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002722 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002723 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002724 void SwapBuf() {
2725 DWORD procID = GetCurrentProcessId();
2726 HWND hwnd = GetTopWindow(GetDesktopWindow());
2727 while(hwnd) {
2728 DWORD wndProcID = 0;
2729 GetWindowThreadProcessId(hwnd, &wndProcID);
2730 if(wndProcID == procID) {
2731 SwapBuffers(GetDC(hwnd));
2732 }
2733 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2734 }
2735 }
2736 #endif
2737#endif
2738
egdaniel0e1853c2016-03-17 11:35:45 -07002739void GrGLGpu::onDraw(const GrPipeline& pipeline,
2740 const GrPrimitiveProcessor& primProc,
2741 const GrMesh* meshes,
2742 int meshCount) {
2743 if (!this->flushGLState(pipeline, primProc)) {
bsalomond95263c2014-12-16 13:05:12 -08002744 return;
2745 }
egdaniel0e1853c2016-03-17 11:35:45 -07002746 GrPixelLocalStorageState plsState = primProc.getPixelLocalStorageState();
2747 if (!fHWPLSEnabled && plsState !=
ethannicholas22793252016-01-30 09:59:10 -08002748 GrPixelLocalStorageState::kDisabled_GrPixelLocalStorageState) {
2749 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
egdaniel0e1853c2016-03-17 11:35:45 -07002750 this->setupPixelLocalStorage(pipeline, primProc);
ethannicholas22793252016-01-30 09:59:10 -08002751 fHWPLSEnabled = true;
2752 }
2753 if (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2754 GrStencilSettings stencil;
2755 stencil.setDisabled();
2756 this->flushStencil(stencil);
2757 }
2758
egdaniel0e1853c2016-03-17 11:35:45 -07002759 for (int i = 0; i < meshCount; ++i) {
2760 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2761 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2762 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002763
egdaniel0e1853c2016-03-17 11:35:45 -07002764 const GrMesh& mesh = meshes[i];
2765 GrMesh::Iterator iter;
2766 const GrNonInstancedMesh* nonIdxMesh = iter.init(mesh);
2767 do {
2768 size_t indexOffsetInBytes = 0;
2769 this->setupGeometry(primProc, *nonIdxMesh, &indexOffsetInBytes);
2770 if (nonIdxMesh->isIndexed()) {
2771 GrGLvoid* indices =
2772 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2773 nonIdxMesh->startIndex());
2774 // info.startVertex() was accounted for by setupGeometry.
2775 GL_CALL(DrawElements(gPrimitiveType2GLMode[nonIdxMesh->primitiveType()],
2776 nonIdxMesh->indexCount(),
2777 GR_GL_UNSIGNED_SHORT,
2778 indices));
2779 } else {
2780 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account
2781 // for startVertex in the DrawElements case. So we always rely on setupGeometry to
2782 // have accounted for startVertex.
2783 GL_CALL(DrawArrays(gPrimitiveType2GLMode[nonIdxMesh->primitiveType()], 0,
2784 nonIdxMesh->vertexCount()));
2785 }
2786 fStats.incNumDraws();
2787 } while ((nonIdxMesh = iter.next()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002788 }
ethannicholas22793252016-01-30 09:59:10 -08002789
2790 if (fHWPLSEnabled && plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2791 // PLS draws always involve multiple draws, finishing up with a non-PLS
2792 // draw that writes to the color buffer. That draw ends up here; we wait
2793 // until after it is complete to actually disable PLS.
2794 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2795 fHWPLSEnabled = false;
2796 this->disableScissor();
2797 }
2798
bsalomon@google.comd302f142011-03-03 13:54:13 +00002799#if SWAP_PER_DRAW
2800 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002801 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002802 aglSwapBuffers(aglGetCurrentContext());
2803 int set_a_break_pt_here = 9;
2804 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002805 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002806 SwapBuf();
2807 int set_a_break_pt_here = 9;
2808 SwapBuf();
2809 #endif
2810#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002811}
2812
cdaltone2e71c22016-04-07 18:13:29 -07002813void GrGLGpu::stampPLSSetupRect(const SkRect& bounds) {
2814 SkASSERT(this->glCaps().glslCaps()->plsPathRenderingSupport())
ethannicholas22793252016-01-30 09:59:10 -08002815
cdaltone2e71c22016-04-07 18:13:29 -07002816 if (!fPLSSetupProgram.fProgram) {
2817 if (!this->createPLSSetupProgram()) {
2818 SkDebugf("Failed to create PLS setup program.\n");
2819 return;
2820 }
2821 }
2822
2823 GL_CALL(UseProgram(fPLSSetupProgram.fProgram));
2824 this->fHWVertexArrayState.setVertexArrayID(this, 0);
2825
2826 GrGLAttribArrayState* attribs = this->fHWVertexArrayState.bindInternalVertexArray(this);
2827 attribs->set(this, 0, fPLSSetupProgram.fArrayBuffer, kVec2f_GrVertexAttribType,
2828 2 * sizeof(GrGLfloat), 0);
ethannicholas22793252016-01-30 09:59:10 -08002829 attribs->disableUnusedArrays(this, 0x1);
2830
cdaltone2e71c22016-04-07 18:13:29 -07002831 GL_CALL(Uniform4f(fPLSSetupProgram.fPosXformUniform, bounds.width(), bounds.height(),
2832 bounds.left(), bounds.top()));
ethannicholas22793252016-01-30 09:59:10 -08002833
2834 GrXferProcessor::BlendInfo blendInfo;
2835 blendInfo.reset();
2836 this->flushBlend(blendInfo, GrSwizzle());
2837 this->flushColorWrite(true);
2838 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
2839 if (!fHWStencilSettings.isDisabled()) {
2840 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2841 }
2842 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
2843 GL_CALL(UseProgram(fHWProgramID));
2844 if (!fHWStencilSettings.isDisabled()) {
2845 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2846 }
2847}
2848
egdaniel0e1853c2016-03-17 11:35:45 -07002849void GrGLGpu::setupPixelLocalStorage(const GrPipeline& pipeline,
2850 const GrPrimitiveProcessor& primProc) {
ethannicholas22793252016-01-30 09:59:10 -08002851 fPLSHasBeenUsed = true;
halcanary9d524f22016-03-29 09:03:52 -07002852 const SkRect& bounds =
egdaniel0e1853c2016-03-17 11:35:45 -07002853 static_cast<const GrPLSGeometryProcessor&>(primProc).getBounds();
ethannicholas22793252016-01-30 09:59:10 -08002854 // setup pixel local storage -- this means capturing and storing the current framebuffer color
2855 // and initializing the winding counts to zero
egdaniel0e1853c2016-03-17 11:35:45 -07002856 GrRenderTarget* rt = pipeline.getRenderTarget();
ethannicholas22793252016-01-30 09:59:10 -08002857 SkScalar width = SkIntToScalar(rt->width());
2858 SkScalar height = SkIntToScalar(rt->height());
2859 // dst rect edges in NDC (-1 to 1)
2860 // having some issues with rounding, just expand the bounds by 1 and trust the scissor to keep
2861 // it contained properly
2862 GrGLfloat dx0 = 2.0f * (bounds.left() - 1) / width - 1.0f;
2863 GrGLfloat dx1 = 2.0f * (bounds.right() + 1) / width - 1.0f;
2864 GrGLfloat dy0 = -2.0f * (bounds.top() - 1) / height + 1.0f;
2865 GrGLfloat dy1 = -2.0f * (bounds.bottom() + 1) / height + 1.0f;
2866 SkRect deviceBounds = SkRect::MakeXYWH(dx0, dy0, dx1 - dx0, dy1 - dy0);
halcanary9d524f22016-03-29 09:03:52 -07002867
ethannicholas22793252016-01-30 09:59:10 -08002868 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
cdaltone2e71c22016-04-07 18:13:29 -07002869 this->stampPLSSetupRect(deviceBounds);
ethannicholas22793252016-01-30 09:59:10 -08002870}
2871
bsalomon861e1032014-12-16 07:33:49 -08002872void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002873 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002874 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002875 // Some extensions automatically resolves the texture when it is read.
2876 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002877 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2878 fStats.incRenderTargetBinds();
2879 fStats.incRenderTargetBinds();
2880 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2881 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2882 // make sure we go through flushRenderTarget() since we've modified
2883 // the bound DRAW FBO ID.
2884 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002885 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002886 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002887
bsalomon@google.com347c3822013-05-01 20:10:01 +00002888 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002889 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002890 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002891 scissorState.set(dirtyRect);
2892 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002893 GL_CALL(ResolveMultisampleFramebuffer());
2894 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002895 GrGLIRect r;
2896 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2897 dirtyRect.width(), dirtyRect.height(), target->origin());
2898
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002899 int right = r.fLeft + r.fWidth;
2900 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002901
2902 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002903 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002904 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2905 r.fLeft, r.fBottom, right, top,
2906 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002907 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002908 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002909 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002910 }
2911}
2912
bsalomon@google.com411dad02012-06-05 20:24:20 +00002913namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002914
bsalomon@google.com411dad02012-06-05 20:24:20 +00002915
2916GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2917 static const GrGLenum gTable[] = {
2918 GR_GL_KEEP, // kKeep_StencilOp
2919 GR_GL_REPLACE, // kReplace_StencilOp
2920 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2921 GR_GL_INCR, // kIncClamp_StencilOp
2922 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2923 GR_GL_DECR, // kDecClamp_StencilOp
2924 GR_GL_ZERO, // kZero_StencilOp
2925 GR_GL_INVERT, // kInvert_StencilOp
2926 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002927 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002928 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2929 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2930 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2931 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2932 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2933 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2934 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2935 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002936 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002937 return gTable[op];
2938}
2939
2940void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002941 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002942 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002943 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002944 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002945 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2946 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2947
2948 GrGLint ref = settings.funcRef(grFace);
2949 GrGLint mask = settings.funcMask(grFace);
2950 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002951
2952 if (GR_GL_FRONT_AND_BACK == glFace) {
2953 // we call the combined func just in case separate stencil is not
2954 // supported.
2955 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2956 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002957 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002958 } else {
2959 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2960 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002961 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002962 }
2963}
2964}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002965
bsalomon3e791242014-12-17 13:43:13 -08002966void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2967 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002968 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002969 if (kNo_TriState != fHWStencilTestEnabled) {
2970 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2971 fHWStencilTestEnabled = kNo_TriState;
2972 }
2973 } else {
2974 if (kYes_TriState != fHWStencilTestEnabled) {
2975 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2976 fHWStencilTestEnabled = kYes_TriState;
2977 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002978 }
joshualitta58fe352014-10-27 08:39:00 -07002979 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002980 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002981 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002982 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002983 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002984 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002985 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002986 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002987 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002988 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002989 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002990 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002991 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002992 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002993 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002994 }
2995 }
joshualitta58fe352014-10-27 08:39:00 -07002996 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002997 }
2998}
2999
cdaltonaf8bc7d2016-02-05 09:35:20 -08003000void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08003001 // rt is only optional if useHWAA is false.
3002 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07003003 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00003004
cdaltond4727922015-11-10 12:49:06 -08003005 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07003006 if (useHWAA) {
3007 if (kYes_TriState != fMSAAEnabled) {
3008 GL_CALL(Enable(GR_GL_MULTISAMPLE));
3009 fMSAAEnabled = kYes_TriState;
3010 }
3011 } else {
3012 if (kNo_TriState != fMSAAEnabled) {
3013 GL_CALL(Disable(GR_GL_MULTISAMPLE));
3014 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003015 }
3016 }
3017 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08003018
3019 if (0 != this->caps()->maxRasterSamples()) {
3020 if (useHWAA && rt->hasMixedSamples() && !stencilEnabled) {
3021 // Since stencil is disabled and we want more samples than are in the color buffer, we
3022 // need to tell the rasterizer explicitly how many to run.
3023 if (kYes_TriState != fHWRasterMultisampleEnabled) {
3024 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
3025 fHWRasterMultisampleEnabled = kYes_TriState;
3026 }
3027 if (rt->numStencilSamples() != fHWNumRasterSamples) {
3028 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
3029 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
3030 fHWNumRasterSamples = rt->numStencilSamples();
3031 }
3032 } else {
3033 if (kNo_TriState != fHWRasterMultisampleEnabled) {
3034 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
3035 fHWRasterMultisampleEnabled = kNo_TriState;
3036 }
3037 }
3038 } else {
3039 SkASSERT(!useHWAA || !rt->hasMixedSamples() || stencilEnabled);
3040 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003041}
3042
bsalomon7f9b2e42016-01-12 13:29:26 -08003043void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07003044 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07003045 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07003046
cdalton8917d622015-05-06 13:40:21 -07003047 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08003048 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
3049 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07003050 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
3051 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07003052 if (blendOff) {
3053 if (kNo_TriState != fHWBlendState.fEnabled) {
3054 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07003055
3056 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
3057 // https://code.google.com/p/skia/issues/detail?id=3943
3058 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
3059 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
3060 SkASSERT(this->caps()->advancedBlendEquationSupport());
3061 // Set to any basic blending equation.
3062 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
3063 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
3064 fHWBlendState.fEquation = blend_equation;
3065 }
3066
egdanielb414f252014-07-29 13:15:47 -07003067 fHWBlendState.fEnabled = kNo_TriState;
3068 }
cdalton8917d622015-05-06 13:40:21 -07003069 return;
3070 }
3071
3072 if (kYes_TriState != fHWBlendState.fEnabled) {
3073 GL_CALL(Enable(GR_GL_BLEND));
3074 fHWBlendState.fEnabled = kYes_TriState;
3075 }
3076
3077 if (fHWBlendState.fEquation != equation) {
3078 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
3079 fHWBlendState.fEquation = equation;
3080 }
3081
3082 if (GrBlendEquationIsAdvanced(equation)) {
3083 SkASSERT(this->caps()->advancedBlendEquationSupport());
3084 // Advanced equations have no other blend state.
3085 return;
3086 }
3087
bsalomone63ffef2016-02-05 07:17:34 -08003088 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07003089 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
3090 gXfermodeCoeff2Blend[dstCoeff]));
3091 fHWBlendState.fSrcCoeff = srcCoeff;
3092 fHWBlendState.fDstCoeff = dstCoeff;
3093 }
3094
bsalomon7f9b2e42016-01-12 13:29:26 -08003095 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
3096 GrColor blendConst = blendInfo.fBlendConstant;
3097 blendConst = swizzle.applyTo(blendConst);
3098 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
3099 GrGLfloat c[4];
3100 GrColorToRGBAFloat(blendConst, c);
3101 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
3102 fHWBlendState.fConstColor = blendConst;
3103 fHWBlendState.fConstColorValid = true;
3104 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00003105 }
3106}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00003107
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003108static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00003109 static const GrGLenum gWrapModes[] = {
3110 GR_GL_CLAMP_TO_EDGE,
3111 GR_GL_REPEAT,
3112 GR_GL_MIRRORED_REPEAT
3113 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00003114 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00003115 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
3116 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
3117 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
3118 return gWrapModes[tm];
3119}
3120
egdanielb7e7d572015-11-04 04:23:53 -08003121static GrGLenum get_component_enum_from_char(char component) {
3122 switch (component) {
3123 case 'r':
3124 return GR_GL_RED;
3125 case 'g':
3126 return GR_GL_GREEN;
3127 case 'b':
3128 return GR_GL_BLUE;
3129 case 'a':
3130 return GR_GL_ALPHA;
3131 default:
3132 SkFAIL("Unsupported component");
3133 return 0;
3134 }
3135}
3136
3137/** If texture swizzling is available using tex parameters then it is preferred over mangling
3138 the generated shader code. This potentially allows greater reuse of cached shaders. */
3139static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08003140 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08003141 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08003142 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08003143 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08003144 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08003145 }
3146}
3147
brianosmana6359362016-03-21 06:55:37 -07003148void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, bool dstConfigAllowsSRGB,
3149 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07003150 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003151
reed856e9d92015-09-30 12:21:45 -07003152#ifdef SK_DEBUG
3153 if (!this->caps()->npotTextureTileSupport()) {
3154 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
3155 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
3156 if (tileX || tileY) {
3157 const int w = texture->width();
3158 const int h = texture->height();
3159 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3160 }
3161 }
3162#endif
3163
bsalomon@google.comb8670992012-07-25 21:27:09 +00003164 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3165 // 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 +00003166 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003167 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003168 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003169 this->onResolveRenderTarget(texRT);
3170 }
3171
bsalomon1c63bf62014-07-22 13:09:46 -07003172 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003173 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003174 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003175 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003176 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003177 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003178 }
3179
bsalomon@google.com4c883782012-06-04 19:05:11 +00003180 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003181 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003182 bool setAll = timestamp < this->getResetTimestamp();
3183 GrGLTexture::TexParams newTexParams;
3184
brianosmana6359362016-03-21 06:55:37 -07003185 if (this->caps()->srgbSupport()) {
3186 // By default, the decision to allow SRGB decode is based on the destination config.
3187 // A texture can override that by specifying a value in GrTextureParams.
3188 newTexParams.fSRGBDecode =
3189 (dstConfigAllowsSRGB || GrTextureParams::kForceAllowSRGB_SRGBMode == params.srgbMode())
3190 ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3191
3192 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
3193 this->setTextureUnit(unitIdx);
3194 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
3195 }
3196 }
3197
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003198 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003199 GR_GL_NEAREST,
3200 GR_GL_LINEAR,
3201 GR_GL_LINEAR_MIPMAP_LINEAR
3202 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003203 static GrGLenum glMagFilterModes[] = {
3204 GR_GL_NEAREST,
3205 GR_GL_LINEAR,
3206 GR_GL_LINEAR
3207 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003208 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003209
3210 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3211 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3212 filterMode = GrTextureParams::kBilerp_FilterMode;
3213 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003214 }
bsalomonefd7d452014-10-23 14:17:46 -07003215
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003216 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3217 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003218
cblume55f2d2d2016-02-26 13:20:48 -08003219 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3220 if (texture->texturePriv().mipMapsAreDirty()) {
3221 GL_CALL(GenerateMipmap(target));
3222 texture->texturePriv().dirtyMipMaps(false);
3223 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3224 texture->width(), texture->height()));
3225 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003226 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003227
cblume55f2d2d2016-02-26 13:20:48 -08003228 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3229
bsalomon@google.comb8670992012-07-25 21:27:09 +00003230 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3231 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003232 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003233 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003234 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003235 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003236 }
3237 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3238 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003239 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003240 }
cblume55f2d2d2016-02-26 13:20:48 -08003241 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003242 // These are not supported in ES2 contexts
3243 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3244 if (newTexParams.fMaxMipMapLevel != 0) {
3245 this->setTextureUnit(unitIdx);
3246 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3247 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3248 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3249 newTexParams.fMaxMipMapLevel));
3250 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3251 newTexParams.fMaxMipMapLevel));
3252 }
cblume55f2d2d2016-02-26 13:20:48 -08003253 }
3254 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003255 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003256 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003257 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003258 }
3259 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003260 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003261 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003262 }
bsalomoncdee0092016-01-08 13:20:12 -08003263 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003264 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3265 oldTexParams.fSwizzleRGBA,
3266 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003267 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003268 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003269 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3270 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
bsalomon10528f12015-10-14 12:54:52 -07003271 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3272 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3273 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3274 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003275 } else {
3276 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
3277 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
bsalomon10528f12015-10-14 12:54:52 -07003278 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003279 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003280 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003281 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003282}
3283
egdaniel080e6732014-12-22 07:35:52 -08003284void GrGLGpu::flushColorWrite(bool writeColor) {
3285 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003286 if (kNo_TriState != fHWWriteToColor) {
3287 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3288 GR_GL_FALSE, GR_GL_FALSE));
3289 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003290 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003291 } else {
3292 if (kYes_TriState != fHWWriteToColor) {
3293 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3294 fHWWriteToColor = kYes_TriState;
3295 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003296 }
bsalomon3e791242014-12-17 13:43:13 -08003297}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003298
egdaniel8dd688b2015-01-22 10:16:09 -08003299void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003300 if (fHWDrawFace != face) {
3301 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08003302 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003303 GL_CALL(Enable(GR_GL_CULL_FACE));
3304 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003305 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003306 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003307 GL_CALL(Enable(GR_GL_CULL_FACE));
3308 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003309 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003310 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003311 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003312 break;
3313 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003314 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003315 }
bsalomon3e791242014-12-17 13:43:13 -08003316 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003317 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003318}
3319
bsalomon861e1032014-12-16 07:33:49 -08003320void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003321 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003322 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003323 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003324 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003325 }
3326}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003327
bsalomon861e1032014-12-16 07:33:49 -08003328void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003329 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003330 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003331 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3332 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3333 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003334 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003335 // clear out the this field so that if a program does use this unit it will rebind the correct
3336 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07003337 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003338}
3339
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003340// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon7ea33f52015-11-22 14:51:00 -08003341static inline bool can_blit_framebuffer(const GrSurface* dst,
3342 const GrSurface* src,
3343 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003344 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
bsalomon083617b2016-02-12 12:10:14 -08003345 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0)) {
3346 switch (gpu->glCaps().blitFramebufferSupport()) {
3347 case GrGLCaps::kNone_BlitFramebufferSupport:
3348 return false;
3349 case GrGLCaps::kNoScalingNoMirroring_BlitFramebufferSupport:
3350 // Our copy surface doesn't support scaling so just check for mirroring.
3351 if (dst->origin() != src->origin()) {
3352 return false;
3353 }
3354 break;
3355 case GrGLCaps::kFull_BlitFramebufferSupport:
3356 break;
3357 }
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00003358 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
3359 // or the rects are not the same (not just the same size but have the same edges).
3360 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
3361 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
3362 return false;
3363 }
bsalomon7ea33f52015-11-22 14:51:00 -08003364 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3365 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3366 return false;
3367 }
3368 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3369 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3370 return false;
3371 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003372 return true;
3373 } else {
3374 return false;
3375 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003376}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003377
bsalomon7ea33f52015-11-22 14:51:00 -08003378static inline bool can_copy_texsubimage(const GrSurface* dst,
3379 const GrSurface* src,
3380 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003381 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3382 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3383 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003384 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003385 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3386 return false;
3387 }
3388 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3389 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3390 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003391 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003392 return false;
3393 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003394 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3395 // If the src is multisampled (and uses an extension where there is a separate MSAA
3396 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003397 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003398 return false;
3399 }
bsalomon7ea33f52015-11-22 14:51:00 -08003400
3401 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3402 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3403 // texture.
3404 if (!dstTex) {
3405 return false;
3406 }
3407
3408 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003409
bsalomon7ea33f52015-11-22 14:51:00 -08003410 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3411 // is required.
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003412 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003413 !GrPixelConfigIsCompressed(src->config()) &&
3414 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) &&
3415 dstTex->target() == GR_GL_TEXTURE_2D &&
3416 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003417 return true;
3418 } else {
3419 return false;
3420 }
3421}
3422
3423// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3424// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07003425void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3426 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003427 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003428 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003429 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003430 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003431 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003432 GrGLuint* tempFBOID;
3433 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003434
egdanield803f272015-03-18 13:01:52 -07003435 if (0 == *tempFBOID) {
3436 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003437 }
3438
egdanield803f272015-03-18 13:01:52 -07003439 fStats.incRenderTargetBinds();
3440 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3441 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003442 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003443 target,
robertphillips754f4e92014-09-18 13:52:08 -07003444 texID,
3445 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003446 viewport->fLeft = 0;
3447 viewport->fBottom = 0;
3448 viewport->fWidth = surface->width();
3449 viewport->fHeight = surface->height();
3450 } else {
egdanield803f272015-03-18 13:01:52 -07003451 fStats.incRenderTargetBinds();
3452 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003453 *viewport = rt->getViewport();
3454 }
egdaniel0f5f9672015-02-03 11:10:51 -08003455}
3456
bsalomon10528f12015-10-14 12:54:52 -07003457void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
cblume61214052016-01-26 09:10:48 -08003458 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003459 if (!surface->asRenderTarget()) {
3460 SkASSERT(surface->asTexture());
3461 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3462 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3463 GR_GL_COLOR_ATTACHMENT0,
3464 textureTarget,
3465 0,
3466 0));
3467 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003468}
3469
joshualitt1c735482015-07-13 08:08:25 -07003470bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07003471 // If the src is a texture, we can implement the blit as a draw assuming the config is
3472 // renderable.
3473 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
3474 desc->fOrigin = kDefault_GrSurfaceOrigin;
3475 desc->fFlags = kRenderTarget_GrSurfaceFlag;
3476 desc->fConfig = src->config();
3477 return true;
3478 }
3479
bsalomon7ea33f52015-11-22 14:51:00 -08003480 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
3481 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
3482 // Not supported for FBO blit or CopyTexSubImage
3483 return false;
3484 }
3485
bsalomon6df86402015-06-01 10:41:49 -07003486 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08003487 // possible and we return false to fallback to creating a render target dst for render-to-
3488 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
3489 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
3490
bsalomon@google.comeb851172013-04-15 13:51:00 +00003491 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003492 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003493 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003494 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
3495 // then we set up for that, otherwise fail.
3496 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
3497 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003498 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003499 desc->fConfig = kBGRA_8888_GrPixelConfig;
3500 return true;
3501 }
3502 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07003503 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003504 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
3505 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00003506 }
3507
3508 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07003509 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003510 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
3511 // fail.
3512 if (this->caps()->isConfigRenderable(src->config(), false)) {
3513 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003514 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003515 desc->fConfig = src->config();
3516 return true;
3517 }
3518 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00003519 }
bsalomonf90a02b2014-11-26 12:28:00 -08003520
3521 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
3522 desc->fConfig = src->config();
3523 desc->fOrigin = src->origin();
3524 desc->fFlags = kNone_GrSurfaceFlags;
3525 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003526}
3527
joshualitt1cbdcde2015-08-21 11:53:29 -07003528bool GrGLGpu::onCopySurface(GrSurface* dst,
3529 GrSurface* src,
3530 const SkIRect& srcRect,
3531 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003532 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3533 // swizzle.
3534 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
3535 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
3536 return false;
3537 }
bsalomon083617b2016-02-12 12:10:14 -08003538 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3539 bool preferCopy = SkToBool(dst->asRenderTarget());
3540 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003541 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3542 return true;
3543 }
bsalomon5df6fee2015-05-18 06:26:15 -07003544 }
cblume61214052016-01-26 09:10:48 -08003545
bsalomon6df86402015-06-01 10:41:49 -07003546 if (can_copy_texsubimage(dst, src, this)) {
3547 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3548 return true;
3549 }
3550
mtklein404b3b22015-05-18 09:29:10 -07003551 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003552 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3553 }
3554
bsalomon083617b2016-02-12 12:10:14 -08003555 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003556 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3557 return true;
3558 }
bsalomon083617b2016-02-12 12:10:14 -08003559 }
3560
bsalomon6df86402015-06-01 10:41:49 -07003561 return false;
3562}
3563
cdaltone2e71c22016-04-07 18:13:29 -07003564bool GrGLGpu::createCopyProgram(int progIdx) {
cdaltonc08f1962016-02-12 12:14:06 -08003565 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
bsalomone5286e02016-01-14 09:24:09 -08003566 static const GrSLType kSamplerTypes[3] = { kSampler2D_GrSLType, kSamplerExternal_GrSLType,
3567 kSampler2DRect_GrSLType };
cdaltone2e71c22016-04-07 18:13:29 -07003568 if (kSamplerExternal_GrSLType == kSamplerTypes[progIdx] &&
3569 !this->glCaps().glslCaps()->externalTextureSupport()) {
3570 return false;
cdaltondeacc972016-04-06 14:26:33 -07003571 }
cdaltone2e71c22016-04-07 18:13:29 -07003572 if (kSampler2DRect_GrSLType == kSamplerTypes[progIdx] &&
3573 !this->glCaps().rectangleTextureSupport()) {
3574 return false;
3575 }
3576
3577 if (!fCopyProgramArrayBuffer) {
3578 static const GrGLfloat vdata[] = {
3579 0, 0,
3580 0, 1,
3581 1, 0,
3582 1, 1
3583 };
3584 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3585 kStatic_GrAccessPattern, vdata));
3586 }
3587 if (!fCopyProgramArrayBuffer) {
3588 return false;
3589 }
3590
3591 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3592 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3593 if (!fCopyPrograms[progIdx].fProgram) {
3594 return false;
3595 }
3596
3597 const char* version = glslCaps->versionDeclString();
3598 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3599 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3600 GrShaderVar::kUniform_TypeModifier);
3601 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
3602 GrShaderVar::kUniform_TypeModifier);
3603 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[progIdx],
3604 GrShaderVar::kUniform_TypeModifier);
3605 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
3606 GrShaderVar::kVaryingOut_TypeModifier);
3607 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3608 GrShaderVar::kOut_TypeModifier);
3609
3610 SkString vshaderTxt(version);
3611 if (glslCaps->noperspectiveInterpolationSupport()) {
3612 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3613 vshaderTxt.appendf("#extension %s : require\n", extension);
3614 }
3615 vTexCoord.addModifier("noperspective");
3616 }
3617
3618 aVertex.appendDecl(glslCaps, &vshaderTxt);
3619 vshaderTxt.append(";");
3620 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
3621 vshaderTxt.append(";");
3622 uPosXform.appendDecl(glslCaps, &vshaderTxt);
3623 vshaderTxt.append(";");
3624 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
3625 vshaderTxt.append(";");
3626
3627 vshaderTxt.append(
3628 "// Copy Program VS\n"
3629 "void main() {"
3630 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3631 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3632 " gl_Position.zw = vec2(0, 1);"
3633 "}"
3634 );
3635
3636 SkString fshaderTxt(version);
3637 if (glslCaps->noperspectiveInterpolationSupport()) {
3638 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3639 fshaderTxt.appendf("#extension %s : require\n", extension);
3640 }
3641 }
3642 if (kSamplerTypes[progIdx] == kSamplerExternal_GrSLType) {
3643 fshaderTxt.appendf("#extension %s : require\n",
3644 glslCaps->externalTextureExtensionString());
3645 }
3646 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
3647 &fshaderTxt);
3648 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3649 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
3650 fshaderTxt.append(";");
3651 uTexture.appendDecl(glslCaps, &fshaderTxt);
3652 fshaderTxt.append(";");
3653 const char* fsOutName;
3654 if (glslCaps->mustDeclareFragmentShaderOutput()) {
3655 oFragColor.appendDecl(glslCaps, &fshaderTxt);
3656 fshaderTxt.append(";");
3657 fsOutName = oFragColor.c_str();
3658 } else {
3659 fsOutName = "gl_FragColor";
3660 }
3661 fshaderTxt.appendf(
3662 "// Copy Program FS\n"
3663 "void main() {"
3664 " %s = %s(u_texture, v_texCoord);"
3665 "}",
3666 fsOutName,
3667 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[progIdx], this->glslGeneration())
3668 );
3669
3670 const char* str;
3671 GrGLint length;
3672
3673 str = vshaderTxt.c_str();
3674 length = SkToInt(vshaderTxt.size());
3675 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3676 GR_GL_VERTEX_SHADER, &str, &length, 1,
3677 &fStats);
3678
3679 str = fshaderTxt.c_str();
3680 length = SkToInt(fshaderTxt.size());
3681 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3682 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3683 &fStats);
3684
3685 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3686
3687 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3688 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3689 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3690 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3691 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3692 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3693
3694 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3695
3696 GL_CALL(DeleteShader(vshader));
3697 GL_CALL(DeleteShader(fshader));
3698
3699 return true;
bsalomon6df86402015-06-01 10:41:49 -07003700}
3701
cdaltone2e71c22016-04-07 18:13:29 -07003702bool GrGLGpu::createWireRectProgram() {
3703 if (!fWireRectArrayBuffer) {
3704 static const GrGLfloat vdata[] = {
3705 0, 0,
3706 0, 1,
3707 1, 1,
3708 1, 0
3709 };
3710 fWireRectArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3711 kStatic_GrAccessPattern, vdata));
3712 if (!fWireRectArrayBuffer) {
3713 return false;
3714 }
3715 }
3716
bsalomon6dea83f2015-12-03 12:58:06 -08003717 SkASSERT(!fWireRectProgram.fProgram);
cdaltone2e71c22016-04-07 18:13:29 -07003718 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3719 if (!fWireRectProgram.fProgram) {
3720 return false;
3721 }
3722
bsalomon6dea83f2015-12-03 12:58:06 -08003723 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3724 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3725 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3726 const char* version = this->glCaps().glslCaps()->versionDeclString();
3727
3728 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3729 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3730 // uniform using the 4 vbo vertices.
3731 SkString vshaderTxt(version);
3732 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3733 vshaderTxt.append(";");
3734 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3735 vshaderTxt.append(";");
3736 vshaderTxt.append(
3737 "// Wire Rect Program VS\n"
3738 "void main() {"
3739 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3740 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3741 " gl_Position.zw = vec2(0, 1);"
3742 "}"
3743 );
3744
3745 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
3746
3747 SkString fshaderTxt(version);
3748 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
3749 *this->glCaps().glslCaps(),
3750 &fshaderTxt);
3751 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3752 fshaderTxt.append(";");
3753 const char* fsOutName;
3754 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3755 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3756 fshaderTxt.append(";");
3757 fsOutName = oFragColor.c_str();
3758 } else {
3759 fsOutName = "gl_FragColor";
3760 }
3761 fshaderTxt.appendf(
3762 "// Write Rect Program FS\n"
3763 "void main() {"
3764 " %s = %s;"
3765 "}",
3766 fsOutName,
3767 uColor.c_str()
3768 );
3769
bsalomon6dea83f2015-12-03 12:58:06 -08003770 const char* str;
3771 GrGLint length;
3772
3773 str = vshaderTxt.c_str();
3774 length = SkToInt(vshaderTxt.size());
3775 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3776 GR_GL_VERTEX_SHADER, &str, &length, 1,
3777 &fStats);
3778
3779 str = fshaderTxt.c_str();
3780 length = SkToInt(fshaderTxt.size());
3781 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3782 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3783 &fStats);
3784
3785 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3786
3787 GL_CALL_RET(fWireRectProgram.fColorUniform,
3788 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3789 GL_CALL_RET(fWireRectProgram.fRectUniform,
3790 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3791 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3792
3793 GL_CALL(DeleteShader(vshader));
3794 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003795
3796 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003797}
3798
3799void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003800 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3801 // visualization.
3802
bsalomon6dea83f2015-12-03 12:58:06 -08003803 this->handleDirtyContext();
3804 if (!fWireRectProgram.fProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003805 if (!this->createWireRectProgram()) {
3806 SkDebugf("Failed to create wire rect program.\n");
3807 return;
3808 }
bsalomon6dea83f2015-12-03 12:58:06 -08003809 }
3810
3811 int w = rt->width();
3812 int h = rt->height();
3813
3814 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3815 // whether the render target is flipped or not.
3816 GrGLfloat edges[4];
3817 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3818 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3819 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3820 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3821 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3822 } else {
3823 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3824 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3825 }
3826 edges[0] = 2 * edges[0] / w - 1.0f;
3827 edges[1] = 2 * edges[1] / h - 1.0f;
3828 edges[2] = 2 * edges[2] / w - 1.0f;
3829 edges[3] = 2 * edges[3] / h - 1.0f;
3830
3831 GrGLfloat channels[4];
3832 static const GrGLfloat scale255 = 1.f / 255.f;
3833 channels[0] = GrColorUnpackR(color) * scale255;
3834 channels[1] = GrColorUnpackG(color) * scale255;
3835 channels[2] = GrColorUnpackB(color) * scale255;
3836 channels[3] = GrColorUnpackA(color) * scale255;
3837
3838 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3839 this->flushRenderTarget(glRT, &rect);
3840
3841 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3842 fHWProgramID = fWireRectProgram.fProgram;
3843
cdaltone2e71c22016-04-07 18:13:29 -07003844 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003845
cdaltone2e71c22016-04-07 18:13:29 -07003846 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
cdalton793dc262016-02-08 10:11:47 -08003847 attribs->set(this, 0, fWireRectArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
3848 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003849 attribs->disableUnusedArrays(this, 0x1);
3850
3851 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3852 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3853
3854 GrXferProcessor::BlendInfo blendInfo;
3855 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003856 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003857 this->flushColorWrite(true);
3858 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003859 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08003860 this->disableScissor();
3861 GrStencilSettings stencil;
3862 stencil.setDisabled();
3863 this->flushStencil(stencil);
3864
3865 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3866}
3867
3868
cdaltone2e71c22016-04-07 18:13:29 -07003869bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07003870 GrSurface* src,
3871 const SkIRect& srcRect,
3872 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003873 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3874 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
3875
3876 if (!fCopyPrograms[progIdx].fProgram) {
3877 if (!this->createCopyProgram(progIdx)) {
3878 SkDebugf("Failed to create copy program.\n");
3879 return false;
3880 }
3881 }
3882
bsalomon6df86402015-06-01 10:41:49 -07003883 int w = srcRect.width();
3884 int h = srcRect.height();
3885
bsalomon6df86402015-06-01 10:41:49 -07003886 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07003887 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003888
bsalomon083617b2016-02-12 12:10:14 -08003889 GrGLIRect dstVP;
3890 this->bindSurfaceFBOForCopy(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3891 this->flushViewport(dstVP);
3892 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3893
bsalomon6df86402015-06-01 10:41:49 -07003894 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003895
bsalomon7ea33f52015-11-22 14:51:00 -08003896 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3897 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003898
cdaltone2e71c22016-04-07 18:13:29 -07003899 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003900
cdaltone2e71c22016-04-07 18:13:29 -07003901 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
cdalton793dc262016-02-08 10:11:47 -08003902 attribs->set(this, 0, fCopyProgramArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
3903 0);
bsalomond6246342015-06-04 13:57:00 -07003904 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003905
3906 // dst rect edges in NDC (-1 to 1)
3907 int dw = dst->width();
3908 int dh = dst->height();
3909 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3910 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3911 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3912 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3913 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3914 dy0 = -dy0;
3915 dy1 = -dy1;
3916 }
3917
bsalomone5286e02016-01-14 09:24:09 -08003918 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3919 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3920 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3921 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07003922 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003923 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003924 sy0 = sh - sy0;
3925 sy1 = sh - sy1;
3926 }
3927 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
3928 GrGLenum srcTarget = srcTex->target();
3929 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
3930 int sw = src->width();
3931 sx0 /= sw;
3932 sx1 /= sw;
3933 sy0 /= sh;
3934 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003935 }
3936
bsalomon7ea33f52015-11-22 14:51:00 -08003937 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3938 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3939 sx1 - sx0, sy1 - sy0, sx0, sy0));
3940 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003941
3942 GrXferProcessor::BlendInfo blendInfo;
3943 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003944 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003945 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003946 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon083617b2016-02-12 12:10:14 -08003947 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003948 this->disableScissor();
3949 GrStencilSettings stencil;
3950 stencil.setDisabled();
3951 this->flushStencil(stencil);
3952
3953 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon083617b2016-02-12 12:10:14 -08003954 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, dst);
3955 this->didWriteToSurface(dst, &dstRect);
3956
cdaltone2e71c22016-04-07 18:13:29 -07003957 return true;
bsalomon6df86402015-06-01 10:41:49 -07003958}
3959
3960void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3961 GrSurface* src,
3962 const SkIRect& srcRect,
3963 const SkIPoint& dstPoint) {
3964 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003965 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003966 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003967 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003968 SkASSERT(dstTex);
3969 // We modified the bound FBO
3970 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3971 GrGLIRect srcGLRect;
3972 srcGLRect.setRelativeTo(srcVP,
3973 srcRect.fLeft,
3974 srcRect.fTop,
3975 srcRect.width(),
3976 srcRect.height(),
3977 src->origin());
3978
3979 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003980 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003981 GrGLint dstY;
3982 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3983 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3984 } else {
3985 dstY = dstPoint.fY;
3986 }
bsalomon10528f12015-10-14 12:54:52 -07003987 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003988 dstPoint.fX, dstY,
3989 srcGLRect.fLeft, srcGLRect.fBottom,
3990 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07003991 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003992 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3993 srcRect.width(), srcRect.height());
3994 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003995}
3996
3997bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3998 GrSurface* src,
3999 const SkIRect& srcRect,
4000 const SkIPoint& dstPoint) {
4001 SkASSERT(can_blit_framebuffer(dst, src, this));
4002 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4003 srcRect.width(), srcRect.height());
4004 if (dst == src) {
4005 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4006 return false;
mtklein404b3b22015-05-18 09:29:10 -07004007 }
bsalomon5df6fee2015-05-18 06:26:15 -07004008 }
bsalomon6df86402015-06-01 10:41:49 -07004009
bsalomon6df86402015-06-01 10:41:49 -07004010 GrGLIRect dstVP;
4011 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07004012 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4013 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004014 // We modified the bound FBO
4015 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4016 GrGLIRect srcGLRect;
4017 GrGLIRect dstGLRect;
4018 srcGLRect.setRelativeTo(srcVP,
4019 srcRect.fLeft,
4020 srcRect.fTop,
4021 srcRect.width(),
4022 srcRect.height(),
4023 src->origin());
4024 dstGLRect.setRelativeTo(dstVP,
4025 dstRect.fLeft,
4026 dstRect.fTop,
4027 dstRect.width(),
4028 dstRect.height(),
4029 dst->origin());
4030
4031 // BlitFrameBuffer respects the scissor, so disable it.
4032 this->disableScissor();
4033
4034 GrGLint srcY0;
4035 GrGLint srcY1;
4036 // Does the blit need to y-mirror or not?
4037 if (src->origin() == dst->origin()) {
4038 srcY0 = srcGLRect.fBottom;
4039 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4040 } else {
4041 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4042 srcY1 = srcGLRect.fBottom;
4043 }
4044 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4045 srcY0,
4046 srcGLRect.fLeft + srcGLRect.fWidth,
4047 srcY1,
4048 dstGLRect.fLeft,
4049 dstGLRect.fBottom,
4050 dstGLRect.fLeft + dstGLRect.fWidth,
4051 dstGLRect.fBottom + dstGLRect.fHeight,
4052 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07004053 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
4054 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004055 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004056 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004057}
4058
cdalton28f45b92016-03-07 13:58:26 -08004059void GrGLGpu::onGetMultisampleSpecs(GrRenderTarget* rt,
4060 const GrStencilSettings& stencil,
4061 int* effectiveSampleCnt,
4062 SkAutoTDeleteArray<SkPoint>* sampleLocations) {
4063 SkASSERT(!rt->hasMixedSamples() || rt->renderTargetPriv().getStencilAttachment() ||
4064 stencil.isDisabled());
4065
4066 this->flushStencil(stencil);
4067 this->flushHWAAState(rt, true, !stencil.isDisabled());
4068 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4069
4070 if (0 != this->caps()->maxRasterSamples()) {
4071 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4072 } else {
4073 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4074 }
4075
4076 SkASSERT(*effectiveSampleCnt >= rt->desc().fSampleCnt);
4077
4078 if (this->caps()->sampleLocationsSupport()) {
4079 sampleLocations->reset(new SkPoint[*effectiveSampleCnt]);
4080 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4081 GrGLfloat pos[2];
4082 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4083 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
4084 (*sampleLocations)[i].set(pos[0], pos[1]);
4085 } else {
4086 (*sampleLocations)[i].set(pos[0], 1 - pos[1]);
4087 }
4088 }
4089 }
4090}
4091
cdalton231c5fd2015-05-13 12:35:36 -07004092void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004093 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004094 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004095 case kTexture_GrXferBarrierType: {
4096 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4097 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4098 // The render target uses separate storage so no need for glTextureBarrier.
4099 // FIXME: The render target will resolve automatically when its texture is bound,
4100 // but we could resolve only the bounds that will be read if we do it here instead.
4101 return;
4102 }
cdalton9954bc32015-04-29 14:17:00 -07004103 SkASSERT(this->caps()->textureBarrierSupport());
4104 GL_CALL(TextureBarrier());
4105 return;
cdalton231c5fd2015-05-13 12:35:36 -07004106 }
cdalton8917d622015-05-06 13:40:21 -07004107 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004108 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004109 this->caps()->blendEquationSupport());
4110 GL_CALL(BlendBarrier());
4111 return;
bsalomoncb02b382015-08-12 11:14:50 -07004112 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004113 }
4114}
4115
jvanverth88957922015-07-14 11:02:52 -07004116GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -08004117 GrPixelConfig config) {
bsalomon926cb022015-12-17 18:15:11 -08004118 if (!this->caps()->isConfigTexturable(config)) {
4119 return false;
4120 }
bsalomon091f60c2015-11-10 11:54:56 -08004121 GrGLTextureInfo* info = new GrGLTextureInfo;
4122 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004123 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004124 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004125 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4126 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004127 GL_CALL(BindTexture(info->fTarget, info->fID));
bsalomone63ffef2016-02-05 07:17:34 -08004128 fHWBoundTextureUniqueIDs[0] = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004129 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4130 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4131 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4132 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004133
bsalomon76148af2016-01-12 11:13:47 -08004134 GrGLenum internalFormat;
4135 GrGLenum externalFormat;
4136 GrGLenum externalType;
4137
4138 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4139 &externalType)) {
4140 delete info;
4141#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4142 return 0;
4143#else
4144 return reinterpret_cast<GrBackendObject>(nullptr);
4145#endif
4146 }
jvanverth672bb7f2015-07-13 07:19:57 -07004147
bsalomon091f60c2015-11-10 11:54:56 -08004148 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004149 externalType, pixels));
4150
bsalomon091f60c2015-11-10 11:54:56 -08004151#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4152 GrGLuint id = info->fID;
4153 delete info;
4154 return id;
4155#else
4156 return reinterpret_cast<GrBackendObject>(info);
4157#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004158}
4159
jvanverth88957922015-07-14 11:02:52 -07004160bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004161#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004162 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004163#else
4164 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
4165#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004166
4167 GrGLboolean result;
4168 GL_CALL_RET(result, IsTexture(texID));
4169
4170 return (GR_GL_TRUE == result);
4171}
4172
bsalomone63ffef2016-02-05 07:17:34 -08004173void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
bsalomon091f60c2015-11-10 11:54:56 -08004174#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004175 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004176#else
4177 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
4178 GrGLuint texID = info->fID;
4179#endif
4180
bsalomon67d76202015-11-11 12:40:42 -08004181 if (!abandonTexture) {
4182 GL_CALL(DeleteTextures(1, &texID));
4183 }
bsalomon091f60c2015-11-10 11:54:56 -08004184
4185#ifndef SK_IGNORE_GL_TEXTURE_TARGET
4186 delete info;
4187#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004188}
4189
joshualitt8fd844f2015-12-02 13:36:47 -08004190void GrGLGpu::resetShaderCacheForTesting() const {
4191 fProgramCache->abandon();
4192}
4193
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004194///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004195
cdaltone2e71c22016-04-07 18:13:29 -07004196GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
4197 const GrGLBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004198 GrGLAttribArrayState* attribState;
4199
cdaltone2e71c22016-04-07 18:13:29 -07004200 if (gpu->glCaps().isCoreProfile()) {
4201 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004202 GrGLuint arrayID;
4203 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4204 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004205 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004206 }
cdaltone2e71c22016-04-07 18:13:29 -07004207 if (ibuf) {
4208 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004209 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004210 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004211 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004212 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004213 if (ibuf) {
4214 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4215 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004216 } else {
4217 this->setVertexArrayID(gpu, 0);
4218 }
4219 int attrCount = gpu->glCaps().maxVertexAttributes();
4220 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4221 fDefaultVertexArrayAttribState.resize(attrCount);
4222 }
4223 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004224 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004225 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004226}
bsalomone179a912016-01-20 06:18:10 -08004227
4228bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
4229 GrTextureProducer::CopyParams* copyParams) const {
4230 if (textureParams.isTiled() ||
4231 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
4232 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
4233 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4234 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
4235 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
4236 copyParams->fWidth = texture->width();
4237 copyParams->fHeight = texture->height();
4238 return true;
4239 }
4240 }
4241 return false;
4242}