blob: b36e8870cd73c977f0c203729023ebac5d3fe89b [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon37dd3312014-11-03 08:47:23 -080011#include "GrGLTextureRenderTarget.h"
bsalomon10e23ca2014-11-25 05:52:06 -080012#include "GrGpuResourceCacheAccess.h"
egdaniel170f90b2014-09-16 12:54:40 -070013#include "GrOptDrawState.h"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "GrSurfacePriv.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000015#include "GrTemplates.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000017#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000018#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000019#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000022#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
cdaltonb85a0aa2014-07-21 15:32:44 -070039
twiz@google.com0f31ca72011-03-18 17:38:11 +000040static const GrGLenum gXfermodeCoeff2Blend[] = {
41 GR_GL_ZERO,
42 GR_GL_ONE,
43 GR_GL_SRC_COLOR,
44 GR_GL_ONE_MINUS_SRC_COLOR,
45 GR_GL_DST_COLOR,
46 GR_GL_ONE_MINUS_DST_COLOR,
47 GR_GL_SRC_ALPHA,
48 GR_GL_ONE_MINUS_SRC_ALPHA,
49 GR_GL_DST_ALPHA,
50 GR_GL_ONE_MINUS_DST_ALPHA,
51 GR_GL_CONSTANT_COLOR,
52 GR_GL_ONE_MINUS_CONSTANT_COLOR,
53 GR_GL_CONSTANT_ALPHA,
54 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000055
56 // extended blend coeffs
57 GR_GL_SRC1_COLOR,
58 GR_GL_ONE_MINUS_SRC1_COLOR,
59 GR_GL_SRC1_ALPHA,
60 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000061};
62
bsalomon861e1032014-12-16 07:33:49 -080063bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000064 static const bool gCoeffReferencesBlendConst[] = {
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 true,
76 true,
77 true,
78 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079
80 // extended blend coeffs
81 false,
82 false,
83 false,
84 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000085 };
86 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000087 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000088 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000089
bsalomon@google.com47059542012-06-06 20:51:20 +000090 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
91 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
92 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
93 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
94 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
97 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
98 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
103 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000104
bsalomon@google.com47059542012-06-06 20:51:20 +0000105 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
108 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000109
110 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000111 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000112 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000113}
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115///////////////////////////////////////////////////////////////////////////////
116
rileya@google.come38160c2012-07-03 18:03:04 +0000117static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000118
bsalomon861e1032014-12-16 07:33:49 -0800119GrGLGpu::GrGLGpu(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000120 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000121 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000122
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000123 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000124 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000125
bsalomon1c63bf62014-07-22 13:09:46 -0700126 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000127
robertphillips@google.com6177e692013-02-28 20:16:25 +0000128 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000130 const GrGLubyte* vendor;
131 const GrGLubyte* renderer;
132 const GrGLubyte* version;
133 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
134 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
135 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800136 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000137 this);
tfarina38406c82014-10-31 07:11:12 -0700138 SkDebugf("------ VENDOR %s\n", vendor);
139 SkDebugf("------ RENDERER %s\n", renderer);
140 SkDebugf("------ VERSION %s\n", version);
141 SkDebugf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700142 ctx.extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700143 SkDebugf("\n");
144 SkDebugf(this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000145 }
146
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000147 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000148
joshualitt2dd1ae02014-12-03 06:24:10 -0800149 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000150
bsalomon@google.comfe676522011-06-17 18:12:21 +0000151 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000152 fHWProgramID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700153
154 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700155 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700156 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000157}
158
bsalomon861e1032014-12-16 07:33:49 -0800159GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000160 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000161 // detach the current program so there is no confusion on OpenGL's part
162 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000163 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164 GL_CALL(UseProgram(0));
165 }
166
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000167 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700168}
169
bsalomon861e1032014-12-16 07:33:49 -0800170void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700171 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700172 fProgramCache->abandon();
173 fHWProgramID = 0;
174 if (this->glCaps().pathRenderingSupport()) {
175 this->glPathRendering()->abandonGpuResources();
176 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000177}
178
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000179///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -0800180GrPixelConfig GrGLGpu::preferredReadPixelsConfig(GrPixelConfig readConfig,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000181 GrPixelConfig surfaceConfig) const {
182 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000183 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000184 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000185 GrBytesPerPixel(readConfig) == 4 &&
186 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000187 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
188 // Perhaps this should be guarded by some compiletime or runtime check.
189 return surfaceConfig;
piotaixre4b23142014-10-02 10:57:53 -0700190 } else if (readConfig == kBGRA_8888_GrPixelConfig
191 && !this->glCaps().readPixelsSupported(
192 this->glInterface(),
193 GR_GL_BGRA,
194 GR_GL_UNSIGNED_BYTE,
195 surfaceConfig
196 )) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000197 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000198 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000199 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000200 }
201}
202
bsalomon861e1032014-12-16 07:33:49 -0800203GrPixelConfig GrGLGpu::preferredWritePixelsConfig(GrPixelConfig writeConfig,
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000204 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000205 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
206 return kBGRA_8888_GrPixelConfig;
207 } else {
208 return writeConfig;
209 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000210}
211
bsalomon861e1032014-12-16 07:33:49 -0800212bool GrGLGpu::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700213 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000214 return false;
215 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000216 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000217 // In general ES2 requires the internal format of the texture and the format of the src
218 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
219 // texture. It depends upon which extension added BGRA. The Apple extension allows it
220 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
221 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000222 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000223 !this->glCaps().bgraIsInternalFormat() &&
224 kBGRA_8888_GrPixelConfig == srcConfig &&
225 kRGBA_8888_GrPixelConfig == texture->config()) {
226 return true;
227 } else {
228 return false;
229 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000230 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000231 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000232 }
233}
234
bsalomon861e1032014-12-16 07:33:49 -0800235bool GrGLGpu::fullReadPixelsIsFasterThanPartial() const {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000236 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
237}
238
bsalomon861e1032014-12-16 07:33:49 -0800239void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000240 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000241 if (resetBits & kMisc_GrGLBackendState) {
242 GL_CALL(Disable(GR_GL_DEPTH_TEST));
243 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000244
bsalomon04ddf892014-11-19 12:36:22 -0800245 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000246 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000248 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000249 // Desktop-only state that we never change
250 if (!this->glCaps().isCoreProfile()) {
251 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
252 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
253 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
254 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
255 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
256 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
257 }
258 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
259 // core profile. This seems like a bug since the core spec removes any mention of
260 // GL_ARB_imaging.
261 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
262 GL_CALL(Disable(GR_GL_COLOR_TABLE));
263 }
264 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
265 // Since ES doesn't support glPointSize at all we always use the VS to
266 // set the point size
267 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
268
269 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
270 // currently part of our gl interface. There are probably others as
271 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000272 }
joshualitt58162332014-08-01 06:44:53 -0700273
274 if (kGLES_GrGLStandard == this->glStandard() &&
275 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
276 // The arm extension requires specifically enabling MSAA fetching per sample.
277 // On some devices this may have a perf hit. Also multiple render targets are disabled
278 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
279 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000280 fHWWriteToColor = kUnknown_TriState;
281 // we only ever use lines in hairline mode
282 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000283 }
edisonn@google.comba669992013-06-28 16:03:21 +0000284
egdanielb414f252014-07-29 13:15:47 -0700285 if (resetBits & kMSAAEnable_GrGLBackendState) {
286 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000287 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000288
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000289 fHWActiveTextureUnitIdx = -1; // invalid
290
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000291 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700292 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
293 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000294 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000296
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000297 if (resetBits & kBlend_GrGLBackendState) {
298 fHWBlendState.invalidate();
299 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000300
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000301 if (resetBits & kView_GrGLBackendState) {
302 fHWScissorSettings.invalidate();
303 fHWViewport.invalidate();
304 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000305
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000306 if (resetBits & kStencil_GrGLBackendState) {
307 fHWStencilSettings.invalidate();
308 fHWStencilTestEnabled = kUnknown_TriState;
309 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000310
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000311 // Vertex
312 if (resetBits & kVertex_GrGLBackendState) {
313 fHWGeometryState.invalidate();
314 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000316 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700317 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000318 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000319
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000320 if (resetBits & kPathRendering_GrGLBackendState) {
321 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700322 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000323 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000324 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000325
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000326 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000327 if (resetBits & kPixelStore_GrGLBackendState) {
328 if (this->glCaps().unpackRowLengthSupport()) {
329 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
330 }
331 if (this->glCaps().packRowLengthSupport()) {
332 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
333 }
334 if (this->glCaps().unpackFlipYSupport()) {
335 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
336 }
337 if (this->glCaps().packFlipYSupport()) {
338 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
339 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000340 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000341
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000342 if (resetBits & kProgram_GrGLBackendState) {
343 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000344 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000347namespace {
348
349GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
350 // By default, GrRenderTargets are GL's normal orientation so that they
351 // can be drawn to by the outside world without the client having
352 // to render upside down.
353 if (kDefault_GrSurfaceOrigin == origin) {
354 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
355 } else {
356 return origin;
357 }
358}
359
360}
361
bsalomon861e1032014-12-16 07:33:49 -0800362GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000363 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000364 return NULL;
365 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000366
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000367 if (0 == desc.fTextureHandle) {
368 return NULL;
369 }
370
bsalomon@google.combcce8922013-03-25 15:38:39 +0000371 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000372 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
373 return NULL;
374 }
375
bsalomonb15b4c12014-10-29 12:41:57 -0700376 GrGLTexture::IDDesc idDesc;
377 GrSurfaceDesc surfDesc;
378
379 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
380 idDesc.fIsWrapped = true;
381
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000382 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700383 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
384 surfDesc.fWidth = desc.fWidth;
385 surfDesc.fHeight = desc.fHeight;
386 surfDesc.fConfig = desc.fConfig;
387 surfDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000388 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000389 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
390 // assuming the old behaviour, which is that backend textures are always
391 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
392 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
393 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700394 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000395 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700396 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000397 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000398
399 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000400 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700401 GrGLRenderTarget::IDDesc rtIDDesc;
402 if (!this->createRenderTargetObjects(surfDesc, idDesc.fTextureID, &rtIDDesc)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000403 return NULL;
404 }
bsalomon37dd3312014-11-03 08:47:23 -0800405 texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000406 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700407 texture = SkNEW_ARGS(GrGLTexture, (this, surfDesc, idDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000408 }
409 if (NULL == texture) {
410 return NULL;
411 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000412
bsalomon@google.come269f212011-11-07 13:29:52 +0000413 return texture;
414}
415
bsalomon861e1032014-12-16 07:33:49 -0800416GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc) {
bsalomonb15b4c12014-10-29 12:41:57 -0700417 GrGLRenderTarget::IDDesc idDesc;
418 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
419 idDesc.fMSColorRenderbufferID = 0;
420 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
kkinnunen91f8e7f2014-12-22 05:57:33 -0800421 idDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000422
bsalomonb15b4c12014-10-29 12:41:57 -0700423 GrSurfaceDesc desc;
424 desc.fConfig = wrapDesc.fConfig;
425 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
426 desc.fWidth = wrapDesc.fWidth;
427 desc.fHeight = wrapDesc.fHeight;
428 desc.fSampleCnt = wrapDesc.fSampleCnt;
429 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
430
bsalomon37dd3312014-11-03 08:47:23 -0800431 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget, (this, desc, idDesc));
bsalomonb15b4c12014-10-29 12:41:57 -0700432 if (wrapDesc.fStencilBits) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000433 GrGLStencilBuffer::Format format;
434 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
435 format.fPacked = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700436 format.fStencilBits = wrapDesc.fStencilBits;
437 format.fTotalBits = wrapDesc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000438 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000439 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
440 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000441 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000442 0,
443 desc.fWidth,
444 desc.fHeight,
445 desc.fSampleCnt,
446 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000447 tgt->setStencilBuffer(sb);
448 sb->unref();
449 }
450 return tgt;
451}
452
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000453////////////////////////////////////////////////////////////////////////////////
454
bsalomon861e1032014-12-16 07:33:49 -0800455bool GrGLGpu::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000456 int left, int top, int width, int height,
457 GrPixelConfig config, const void* buffer,
458 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000459 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000460 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000461 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000462 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
463
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000464 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000465 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000466
krajcevski145d48c2014-06-11 16:07:50 -0700467 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700468 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800469 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700470 SkASSERT(config == glTex->desc().fConfig);
471 success = this->uploadCompressedTexData(glTex->desc(), buffer, false, left, top, width,
472 height);
krajcevski145d48c2014-06-11 16:07:50 -0700473 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700474 success = this->uploadTexData(glTex->desc(), false, left, top, width, height, config,
475 buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700476 }
477
478 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700479 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000480 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000481 }
krajcevski145d48c2014-06-11 16:07:50 -0700482
483 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484}
485
bsalomonb15b4c12014-10-29 12:41:57 -0700486static bool adjust_pixel_ops_params(int surfaceWidth,
487 int surfaceHeight,
488 size_t bpp,
489 int* left, int* top, int* width, int* height,
490 const void** data,
491 size_t* rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000492 if (!*rowBytes) {
493 *rowBytes = *width * bpp;
494 }
495
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000496 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
497 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000498
499 if (!subRect.intersect(bounds)) {
500 return false;
501 }
502 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
503 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
504
505 *left = subRect.fLeft;
506 *top = subRect.fTop;
507 *width = subRect.width();
508 *height = subRect.height();
509 return true;
510}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000511
bsalomonb15b4c12014-10-29 12:41:57 -0700512static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
513 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700514 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000515 return GR_GL_GET_ERROR(interface);
516 } else {
517 return CHECK_ALLOC_ERROR(interface);
518 }
519}
520
bsalomon861e1032014-12-16 07:33:49 -0800521bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000522 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000523 int left, int top, int width, int height,
524 GrPixelConfig dataConfig,
525 const void* data,
526 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700527 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000528
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000529 // If we're uploading compressed data then we should be using uploadCompressedTexData
530 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
531
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000532 size_t bpp = GrBytesPerPixel(dataConfig);
533 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
534 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000535 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000536 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000537 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000538
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000539 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700540 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000541
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000542 // We currently lazily create MIPMAPs when the we see a draw with
543 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
544 // MIP levels are all created when the texture is created. So for now we don't use
545 // texture storage.
546 bool useTexStorage = false &&
547 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000548 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000549
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000550 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000551 // 565 is not a sized internal format on desktop GL. So on desktop with
552 // 565 we always use an unsized internal format to let the system pick
553 // the best sized format to convert the 565 data to. Since TexStorage
554 // only allows sized internal formats we will instead use TexImage2D.
555 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000556 }
557
jvanverth3f801cb2014-12-16 09:49:38 -0800558 GrGLenum internalFormat = 0x0; // suppress warning
559 GrGLenum externalFormat = 0x0; // suppress warning
560 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700561
jvanverthe1869ca2014-12-16 13:32:27 -0800562 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800563 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800564 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800565 if (kGL_GrGLStandard == this->glStandard() ||
566 (this->glVersion() >= GR_GL_VER(3, 0) &&
567 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
568 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800569 useSizedFormat = true;
570 }
jvanverthf72b7522014-12-17 10:46:01 -0800571
bsalomone904c092014-07-17 10:50:59 -0700572 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000573 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000574 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000575 }
576
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000577 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000578 * check whether to allocate a temporary buffer for flipping y or
579 * because our srcData has extra bytes past each row. If so, we need
580 * to trim those off here, since GL ES may not let us specify
581 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000582 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000583 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000584 bool swFlipY = false;
585 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700586 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000587 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000588 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000589 glFlipY = true;
590 } else {
591 swFlipY = true;
592 }
593 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000594 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000595 // can't use this for flipping, only non-neg values allowed. :(
596 if (rowBytes != trimRowBytes) {
597 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
598 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
599 restoreGLRowLength = true;
600 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000601 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000602 if (trimRowBytes != rowBytes || swFlipY) {
603 // copy data into our new storage, skipping the trailing bytes
604 size_t trimSize = height * trimRowBytes;
605 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000606 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000607 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000608 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000609 char* dst = (char*)tempStorage.reset(trimSize);
610 for (int y = 0; y < height; y++) {
611 memcpy(dst, src, trimRowBytes);
612 if (swFlipY) {
613 src -= rowBytes;
614 } else {
615 src += rowBytes;
616 }
617 dst += trimRowBytes;
618 }
619 // now point data to our copied version
620 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000621 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000622 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000623 if (glFlipY) {
624 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
625 }
joshualittee5da552014-07-16 13:32:56 -0700626 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
627 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000628 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000630 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000631 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000632 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000633 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000634 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000635 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000636 GL_ALLOC_CALL(this->glInterface(),
637 TexStorage2D(GR_GL_TEXTURE_2D,
638 1, // levels
639 internalFormat,
640 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000641 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700642 GL_ALLOC_CALL(this->glInterface(),
643 TexImage2D(GR_GL_TEXTURE_2D,
644 0, // level
645 internalFormat,
646 desc.fWidth, desc.fHeight,
647 0, // border
648 externalFormat, externalType,
649 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000650 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000651 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000652 if (error != GR_GL_NO_ERROR) {
653 succeeded = false;
654 } else {
655 // if we have data and we used TexStorage to create the texture, we
656 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700657 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000658 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
659 0, // level
660 left, top,
661 width, height,
662 externalFormat, externalType,
663 data));
664 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000665 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000666 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000667 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000668 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000669 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000670 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
671 0, // level
672 left, top,
673 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000674 externalFormat, externalType, data));
675 }
676
677 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000678 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000679 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000680 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000681 if (glFlipY) {
682 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
683 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000684 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000685}
686
krajcevski145d48c2014-06-11 16:07:50 -0700687// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700688// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700689// create a CompressedTexData struct that takes a desc/ptr and figures out
690// the proper upload semantics. Then users can construct this function how they
691// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800692bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700693 const void* data,
694 bool isNewTexture,
695 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700696 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700697
698 // No support for software flip y, yet...
699 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
700
krajcevski145d48c2014-06-11 16:07:50 -0700701 if (-1 == width) {
702 width = desc.fWidth;
703 }
704#ifdef SK_DEBUG
705 else {
706 SkASSERT(width <= desc.fWidth);
707 }
708#endif
709
710 if (-1 == height) {
711 height = desc.fHeight;
712 }
713#ifdef SK_DEBUG
714 else {
715 SkASSERT(height <= desc.fHeight);
716 }
717#endif
718
krajcevski9c0e6292014-06-02 07:38:14 -0700719 // Make sure that the width and height that we pass to OpenGL
720 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800721 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700722
723 // We only need the internal format for compressed 2D textures.
724 GrGLenum internalFormat = 0;
725 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
726 return false;
727 }
728
krajcevski145d48c2014-06-11 16:07:50 -0700729 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700730 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700731 GL_ALLOC_CALL(this->glInterface(),
732 CompressedTexImage2D(GR_GL_TEXTURE_2D,
733 0, // level
734 internalFormat,
735 width, height,
736 0, // border
bsalomon98806072014-12-12 15:11:17 -0800737 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700738 data));
bsalomond4cb9222014-08-11 14:19:09 -0700739 GrGLenum error = check_alloc_error(desc, this->glInterface());
740 if (error != GR_GL_NO_ERROR) {
741 return false;
742 }
krajcevski145d48c2014-06-11 16:07:50 -0700743 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700744 // Paletted textures can't be updated.
745 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
746 return false;
747 }
748 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
749 0, // level
750 left, top,
751 width, height,
752 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800753 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700754 data));
krajcevski145d48c2014-06-11 16:07:50 -0700755 }
krajcevski9c0e6292014-06-02 07:38:14 -0700756
bsalomond4cb9222014-08-11 14:19:09 -0700757 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700758}
759
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000760static bool renderbuffer_storage_msaa(GrGLContext& ctx,
761 int sampleCount,
762 GrGLenum format,
763 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000764 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000765 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000766 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000767 case GrGLCaps::kDesktop_ARB_MSFBOType:
768 case GrGLCaps::kDesktop_EXT_MSFBOType:
769 case GrGLCaps::kES_3_0_MSFBOType:
770 GL_ALLOC_CALL(ctx.interface(),
771 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
772 sampleCount,
773 format,
774 width, height));
775 break;
776 case GrGLCaps::kES_Apple_MSFBOType:
777 GL_ALLOC_CALL(ctx.interface(),
778 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
779 sampleCount,
780 format,
781 width, height));
782 break;
783 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
784 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
785 GL_ALLOC_CALL(ctx.interface(),
786 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
787 sampleCount,
788 format,
789 width, height));
790 break;
791 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000792 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000793 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000794 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000795 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000796}
797
bsalomon861e1032014-12-16 07:33:49 -0800798bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, GrGLuint texID,
bsalomonb15b4c12014-10-29 12:41:57 -0700799 GrGLRenderTarget::IDDesc* idDesc) {
800 idDesc->fMSColorRenderbufferID = 0;
801 idDesc->fRTFBOID = 0;
802 idDesc->fTexFBOID = 0;
803 idDesc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000804
805 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000806
bsalomon@google.comab15d612011-08-09 12:57:56 +0000807 GrGLenum msColorFormat = 0; // suppress warning
808
bsalomonb15b4c12014-10-29 12:41:57 -0700809 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000810 goto FAILED;
811 }
812
bsalomonb15b4c12014-10-29 12:41:57 -0700813 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
814 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000815 goto FAILED;
816 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000817
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000818
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000819 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
820 // the texture bound to the other. The exception is the IMG multisample extension. With this
821 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
822 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700823 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
824 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
825 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
826 if (!idDesc->fRTFBOID ||
827 !idDesc->fMSColorRenderbufferID ||
828 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000829 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000830 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000831 &msColorFormat,
832 NULL,
833 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000834 goto FAILED;
835 }
836 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700837 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 }
839
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000840 // below here we may bind the FBO
bsalomon1c63bf62014-07-22 13:09:46 -0700841 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomonb15b4c12014-10-29 12:41:57 -0700842 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
843 SkASSERT(desc.fSampleCnt > 0);
844 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000845 if (!renderbuffer_storage_msaa(fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700846 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000847 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700848 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000849 goto FAILED;
850 }
robertphillips754f4e92014-09-18 13:52:08 -0700851 fGPUStats.incRenderTargetBinds();
bsalomonb15b4c12014-10-29 12:41:57 -0700852 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000853 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000854 GR_GL_COLOR_ATTACHMENT0,
855 GR_GL_RENDERBUFFER,
bsalomonb15b4c12014-10-29 12:41:57 -0700856 idDesc->fMSColorRenderbufferID));
857 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
858 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000859 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
860 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
861 goto FAILED;
862 }
bsalomonb15b4c12014-10-29 12:41:57 -0700863 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000864 }
865 }
robertphillips754f4e92014-09-18 13:52:08 -0700866 fGPUStats.incRenderTargetBinds();
bsalomonb15b4c12014-10-29 12:41:57 -0700867 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000868
bsalomonb15b4c12014-10-29 12:41:57 -0700869 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000870 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
871 GR_GL_COLOR_ATTACHMENT0,
872 GR_GL_TEXTURE_2D,
bsalomonb15b4c12014-10-29 12:41:57 -0700873 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000874 } else {
875 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
876 GR_GL_COLOR_ATTACHMENT0,
877 GR_GL_TEXTURE_2D,
878 texID, 0));
879 }
bsalomonb15b4c12014-10-29 12:41:57 -0700880 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
881 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000882 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
883 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
884 goto FAILED;
885 }
bsalomonb15b4c12014-10-29 12:41:57 -0700886 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000887 }
888
889 return true;
890
891FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700892 if (idDesc->fMSColorRenderbufferID) {
893 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894 }
bsalomonb15b4c12014-10-29 12:41:57 -0700895 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
896 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 }
bsalomonb15b4c12014-10-29 12:41:57 -0700898 if (idDesc->fTexFBOID) {
899 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900 }
901 return false;
902}
903
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000904// good to set a break-point here to know when createTexture fails
905static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000906// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000907 return NULL;
908}
909
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000910#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000911static size_t as_size_t(int x) {
912 return x;
913}
914#endif
915
bsalomon861e1032014-12-16 07:33:49 -0800916GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000917 const void* srcData,
918 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000919
bsalomonb15b4c12014-10-29 12:41:57 -0700920 GrSurfaceDesc desc = origDesc;
921 GrGLRenderTarget::IDDesc rtIDDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000922
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000923 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000924 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000925 // We fail if the MSAA was requested and is not available.
926 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700927 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000928 return return_null_texture();
929 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000930
bsalomonf2703d82014-10-28 14:33:06 -0700931 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000932
bsalomonb15b4c12014-10-29 12:41:57 -0700933 // If the sample count exceeds the max then we clamp it.
934 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
935 desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000936
bsalomonb15b4c12014-10-29 12:41:57 -0700937 rtIDDesc.fMSColorRenderbufferID = 0;
938 rtIDDesc.fRTFBOID = 0;
939 rtIDDesc.fTexFBOID = 0;
940 rtIDDesc.fIsWrapped = false;
941
942 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700943 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000944 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000945 }
946
reed@google.comac10a2d2010-12-22 21:39:39 +0000947 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000948 int maxRTSize = this->caps()->maxRenderTargetSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700949 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000950 return return_null_texture();
951 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000952 } else {
953 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700954 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000955 return return_null_texture();
956 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000957 }
958
bsalomonb15b4c12014-10-29 12:41:57 -0700959 GrGLTexture::IDDesc idDesc;
960 GL_CALL(GenTextures(1, &idDesc.fTextureID));
961 idDesc.fIsWrapped = false;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000962
bsalomonb15b4c12014-10-29 12:41:57 -0700963 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000964 return return_null_texture();
965 }
966
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000967 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -0700968 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000969
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000970 if (renderTarget && this->glCaps().textureUsageSupport()) {
971 // provides a hint about how this texture will be used
972 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
973 GR_GL_TEXTURE_USAGE,
974 GR_GL_FRAMEBUFFER_ATTACHMENT));
975 }
976
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000977 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
978 // drivers have a bug where an FBO won't be complete if it includes a
979 // texture that is not mipmap complete (considering the filter in use).
980 GrGLTexture::TexParams initialTexParams;
981 // we only set a subset here so invalidate first
982 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000983 initialTexParams.fMinFilter = GR_GL_NEAREST;
984 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000985 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
986 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000987 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
988 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000989 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
991 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000992 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
994 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000995 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000996 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
997 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000998 initialTexParams.fWrapT));
bsalomonb15b4c12014-10-29 12:41:57 -0700999 if (!this->uploadTexData(desc, true, 0, 0,
1000 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001001 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001002 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001003 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001004 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001005
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001006 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001008 // unbind the texture from the texture unit before binding it to the frame buffer
1009 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1010
bsalomonb15b4c12014-10-29 12:41:57 -07001011 if (!this->createRenderTargetObjects(desc, idDesc.fTextureID, &rtIDDesc)) {
1012 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 return return_null_texture();
1014 }
bsalomon37dd3312014-11-03 08:47:23 -08001015 tex = SkNEW_ARGS(GrGLTextureRenderTarget, (this, desc, idDesc, rtIDDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 } else {
bsalomonb15b4c12014-10-29 12:41:57 -07001017 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001019 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001020#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001021 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001022 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001023#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001024 return tex;
1025}
1026
bsalomon861e1032014-12-16 07:33:49 -08001027GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001028
bsalomonb15b4c12014-10-29 12:41:57 -07001029 if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
krajcevski9c0e6292014-06-02 07:38:14 -07001030 return return_null_texture();
1031 }
1032
1033 // Make sure that we're not flipping Y.
bsalomonb15b4c12014-10-29 12:41:57 -07001034 GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
krajcevski9c0e6292014-06-02 07:38:14 -07001035 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1036 return return_null_texture();
1037 }
bsalomonb15b4c12014-10-29 12:41:57 -07001038 GrSurfaceDesc desc = origDesc;
1039 desc.fOrigin = texOrigin;
krajcevski9c0e6292014-06-02 07:38:14 -07001040
1041 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -07001042 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
krajcevski9c0e6292014-06-02 07:38:14 -07001043 return return_null_texture();
1044 }
1045
bsalomonb15b4c12014-10-29 12:41:57 -07001046 GrGLTexture::IDDesc idDesc;
1047 GL_CALL(GenTextures(1, &idDesc.fTextureID));
1048 idDesc.fIsWrapped = false;
krajcevski9c0e6292014-06-02 07:38:14 -07001049
bsalomonb15b4c12014-10-29 12:41:57 -07001050 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001051 return return_null_texture();
1052 }
1053
1054 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -07001055 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001056
1057 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1058 // drivers have a bug where an FBO won't be complete if it includes a
1059 // texture that is not mipmap complete (considering the filter in use).
1060 GrGLTexture::TexParams initialTexParams;
1061 // we only set a subset here so invalidate first
1062 initialTexParams.invalidate();
1063 initialTexParams.fMinFilter = GR_GL_NEAREST;
1064 initialTexParams.fMagFilter = GR_GL_NEAREST;
1065 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1066 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1067 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1068 GR_GL_TEXTURE_MAG_FILTER,
1069 initialTexParams.fMagFilter));
1070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1071 GR_GL_TEXTURE_MIN_FILTER,
1072 initialTexParams.fMinFilter));
1073 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1074 GR_GL_TEXTURE_WRAP_S,
1075 initialTexParams.fWrapS));
1076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1077 GR_GL_TEXTURE_WRAP_T,
1078 initialTexParams.fWrapT));
1079
bsalomonb15b4c12014-10-29 12:41:57 -07001080 if (!this->uploadCompressedTexData(desc, srcData)) {
1081 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001082 return return_null_texture();
1083 }
1084
1085 GrGLTexture* tex;
bsalomonb15b4c12014-10-29 12:41:57 -07001086 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
krajcevski9c0e6292014-06-02 07:38:14 -07001087 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1088#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001089 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001090 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1091#endif
1092 return tex;
1093}
1094
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001096
1097const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1098
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001099void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001100 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001101
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001103 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 (kUnknownBitCount == format->fTotalBits));
1105 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001106 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001107 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1108 (GrGLint*)&format->fStencilBits);
1109 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1112 (GrGLint*)&format->fTotalBits);
1113 format->fTotalBits += format->fStencilBits;
1114 } else {
1115 format->fTotalBits = format->fStencilBits;
1116 }
1117 }
1118}
1119}
1120
bsalomon861e1032014-12-16 07:33:49 -08001121bool GrGLGpu::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122
1123 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001124 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001125 SkASSERT(rt->asTexture());
1126 SkASSERT(width >= rt->width());
1127 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128
1129 int samples = rt->numSamples();
bsalomon12299ab2014-11-14 13:33:09 -08001130 GrGLuint sbID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001132 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon12299ab2014-11-14 13:33:09 -08001134 if (!sbID) {
1135 GL_CALL(GenRenderbuffers(1, &sbID));
1136 }
1137 if (!sbID) {
1138 return false;
1139 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 // we start with the last stencil format that succeeded in hopes
1142 // that we won't go through this loop more than once after the
1143 // first (painful) stencil creation.
1144 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon12299ab2014-11-14 13:33:09 -08001145 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001146 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001147 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001149 bool created;
1150 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001151 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001152 samples,
1153 sFmt.fInternalFormat,
1154 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 } else {
bsalomon12299ab2014-11-14 13:33:09 -08001156 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1157 sFmt.fInternalFormat,
1158 width, height));
1159 created = (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 if (created) {
1162 // After sized formats we attempt an unsized format and take
1163 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001164 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001165 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001166 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001167 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001168 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001169 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1171 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001172 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 return true;
bsalomon12299ab2014-11-14 13:33:09 -08001174 }
bsalomon10e23ca2014-11-25 05:52:06 -08001175 // Remove the scratch key from this resource so we don't grab it from the cache ever
1176 // again.
1177 sb->cacheAccess().removeScratchKey();
1178 // Set this to 0 since we handed the valid ID off to the failed stencil buffer resource.
1179 sbID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 }
1181 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001183 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184}
1185
bsalomon861e1032014-12-16 07:33:49 -08001186bool GrGLGpu::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon37dd3312014-11-03 08:47:23 -08001187 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188
1189 GrGLuint fbo = glrt->renderFBOID();
1190
1191 if (NULL == sb) {
bsalomon49f085d2014-09-05 13:34:00 -07001192 if (rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001194 GR_GL_STENCIL_ATTACHMENT,
1195 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001197 GR_GL_DEPTH_ATTACHMENT,
1198 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001199#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001200 GrGLenum status;
1201 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001202 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203#endif
1204 }
1205 return true;
1206 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001207 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GrGLuint rb = glsb->renderbufferID();
1209
bsalomon1c63bf62014-07-22 13:09:46 -07001210 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
robertphillips754f4e92014-09-18 13:52:08 -07001211 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001212 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001214 GR_GL_STENCIL_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001218 GR_GL_DEPTH_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001222 GR_GL_DEPTH_ATTACHMENT,
1223 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001226 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001227 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001228 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1229 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001231 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001233 if (glsb->format().fPacked) {
1234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1235 GR_GL_DEPTH_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, 0));
1237 }
1238 return false;
1239 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001240 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 rt->config(),
1242 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001245 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001247}
1248
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001249////////////////////////////////////////////////////////////////////////////////
1250
bsalomon861e1032014-12-16 07:33:49 -08001251GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001252 GrGLVertexBuffer::Desc desc;
1253 desc.fDynamic = dynamic;
1254 desc.fSizeInBytes = size;
1255 desc.fIsWrapped = false;
1256
bsalomon@google.com96966a52013-02-21 16:34:21 +00001257 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001258 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001259 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001261 } else {
1262 GL_CALL(GenBuffers(1, &desc.fID));
1263 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001264 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001265 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1266 // make sure driver can allocate memory for this buffer
1267 GL_ALLOC_CALL(this->glInterface(),
1268 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001269 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001270 NULL, // data ptr
1271 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1272 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1273 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001274 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001275 return NULL;
1276 }
1277 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1278 return vertexBuffer;
1279 }
1280 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001282}
1283
bsalomon861e1032014-12-16 07:33:49 -08001284GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001285 GrGLIndexBuffer::Desc desc;
1286 desc.fDynamic = dynamic;
1287 desc.fSizeInBytes = size;
1288 desc.fIsWrapped = false;
1289
bsalomon@google.com96966a52013-02-21 16:34:21 +00001290 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001291 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001292 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001294 } else {
1295 GL_CALL(GenBuffers(1, &desc.fID));
1296 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001297 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001298 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1299 // make sure driver can allocate memory for this buffer
1300 GL_ALLOC_CALL(this->glInterface(),
1301 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001302 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001303 NULL, // data ptr
1304 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1305 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1306 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001307 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001308 return NULL;
1309 }
1310 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1311 return indexBuffer;
1312 }
1313 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001315}
1316
bsalomon3e791242014-12-17 13:43:13 -08001317void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001318 const GrGLIRect& rtViewport,
1319 GrSurfaceOrigin rtOrigin) {
1320 if (scissorState.fEnabled) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001321 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001322 scissor.setRelativeTo(rtViewport,
joshualitt77b13072014-10-27 14:51:01 -07001323 scissorState.fRect.fLeft,
1324 scissorState.fRect.fTop,
1325 scissorState.fRect.width(),
1326 scissorState.fRect.height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001327 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001328 // if the scissor fully contains the viewport then we fall through and
1329 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001330 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001331 if (fHWScissorSettings.fRect != scissor) {
1332 scissor.pushToGLScissor(this->glInterface());
1333 fHWScissorSettings.fRect = scissor;
1334 }
1335 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1336 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1337 fHWScissorSettings.fEnabled = kYes_TriState;
1338 }
1339 return;
1340 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 }
joshualitt77b13072014-10-27 14:51:01 -07001342
1343 // See fall through note above
1344 this->disableScissor();
1345}
1346
bsalomond95263c2014-12-16 13:05:12 -08001347bool GrGLGpu::flushGLState(const GrOptDrawState& optState) {
egdaniel080e6732014-12-22 07:35:52 -08001348 GrXferProcessor::BlendInfo blendInfo;
1349 optState.getXferProcessor()->getBlendInfo(&blendInfo);
1350
bsalomon3e791242014-12-17 13:43:13 -08001351 this->flushDither(optState.isDitherState());
egdaniel080e6732014-12-22 07:35:52 -08001352 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon3e791242014-12-17 13:43:13 -08001353 this->flushDrawFace(optState.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001354
bsalomon1f78c0a2014-12-17 09:43:13 -08001355 fCurrentProgram.reset(fProgramCache->getProgram(optState));
1356 if (NULL == fCurrentProgram.get()) {
1357 SkDEBUGFAIL("Failed to create program!");
1358 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001359 }
1360
bsalomon1f78c0a2014-12-17 09:43:13 -08001361 fCurrentProgram.get()->ref();
1362
1363 GrGLuint programID = fCurrentProgram->programID();
1364 if (fHWProgramID != programID) {
1365 GL_CALL(UseProgram(programID));
1366 fHWProgramID = programID;
1367 }
1368
egdaniel080e6732014-12-22 07:35:52 -08001369 if (blendInfo.fWriteColor) {
1370 this->flushBlend(blendInfo);
1371 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001372
1373 fCurrentProgram->setData(optState);
1374
bsalomonbc3d0de2014-12-15 13:45:03 -08001375 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(optState.getRenderTarget());
bsalomon3e791242014-12-17 13:43:13 -08001376 this->flushStencil(optState.getStencil());
bsalomonbc3d0de2014-12-15 13:45:03 -08001377 this->flushScissor(optState.getScissorState(), glRT->getViewport(), glRT->origin());
bsalomon3e791242014-12-17 13:43:13 -08001378 this->flushHWAAState(glRT, optState.isHWAntialiasState(),
1379 kDrawLines_DrawType == optState.drawType());
bsalomonbc3d0de2014-12-15 13:45:03 -08001380
1381 // This must come after textures are flushed because a texture may need
1382 // to be msaa-resolved (which will modify bound FBO state).
1383 this->flushRenderTarget(glRT, NULL);
1384
1385 return true;
1386}
1387
bsalomon861e1032014-12-16 07:33:49 -08001388void GrGLGpu::setupGeometry(const GrOptDrawState& optState,
bsalomonbc3d0de2014-12-15 13:45:03 -08001389 const GrDrawTarget::DrawInfo& info,
1390 size_t* indexOffsetInBytes) {
1391 GrGLVertexBuffer* vbuf;
1392 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
1393
1394 SkASSERT(vbuf);
1395 SkASSERT(!vbuf->isMapped());
1396
1397 GrGLIndexBuffer* ibuf = NULL;
1398 if (info.isIndexed()) {
1399 SkASSERT(indexOffsetInBytes);
1400
1401 *indexOffsetInBytes = 0;
1402 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
1403
1404 SkASSERT(ibuf);
1405 SkASSERT(!ibuf->isMapped());
1406 *indexOffsetInBytes += ibuf->baseOffset();
1407 }
1408 GrGLAttribArrayState* attribState =
1409 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1410
1411 if (fCurrentProgram->hasVertexShader()) {
1412 const GrGeometryProcessor* gp = optState.getGeometryProcessor();
1413
1414 GrGLsizei stride = static_cast<GrGLsizei>(gp->getVertexStride());
1415
1416 size_t vertexOffsetInBytes = stride * info.startVertex();
1417
1418 vertexOffsetInBytes += vbuf->baseOffset();
1419
1420 const SkTArray<GrGeometryProcessor::GrAttribute, true>& attribs = gp->getAttribs();
1421 int vaCount = attribs.count();
1422 uint32_t usedAttribArraysMask = 0;
1423 size_t offset = 0;
1424
1425 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
1426 usedAttribArraysMask |= (1 << attribIndex);
1427 GrVertexAttribType attribType = attribs[attribIndex].fType;
1428 attribState->set(this,
1429 attribIndex,
1430 vbuf,
1431 GrGLAttribTypeToLayout(attribType).fCount,
1432 GrGLAttribTypeToLayout(attribType).fType,
1433 GrGLAttribTypeToLayout(attribType).fNormalized,
1434 stride,
1435 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
1436 offset += attribs[attribIndex].fOffset;
1437 }
1438 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1439 }
1440}
1441
bsalomon861e1032014-12-16 07:33:49 -08001442void GrGLGpu::buildProgramDesc(const GrOptDrawState& optState,
bsalomonbc3d0de2014-12-15 13:45:03 -08001443 const GrProgramDesc::DescInfo& descInfo,
1444 GrGpu::DrawType drawType,
1445 GrProgramDesc* desc) {
1446 if (!GrGLProgramDescBuilder::Build(optState, descInfo, drawType, this, desc)) {
1447 SkDEBUGFAIL("Failed to generate GL program descriptor");
1448 }
1449}
1450
bsalomon861e1032014-12-16 07:33:49 -08001451void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001452 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001453 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001454 fHWScissorSettings.fEnabled = kNo_TriState;
1455 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001456 }
1457}
1458
bsalomon861e1032014-12-16 07:33:49 -08001459void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
joshualitt4b68ec02014-11-07 14:11:45 -08001460 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001461 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001462 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001463 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001464
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001465 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1466 rect = NULL;
1467 }
1468
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001469 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001470 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001471 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001472 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001473 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001474 if (clippedRect.intersect(rtRect)) {
1475 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001476 } else {
1477 return;
1478 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001479 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001480
bsalomonb0bd4f62014-09-03 07:19:50 -07001481 this->flushRenderTarget(glRT, rect);
bsalomon3e791242014-12-17 13:43:13 -08001482 GrScissorState scissorState;
joshualitt77b13072014-10-27 14:51:01 -07001483 scissorState.fEnabled = SkToBool(rect);
1484 if (scissorState.fEnabled) {
1485 scissorState.fRect = *rect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001486 }
joshualitt77b13072014-10-27 14:51:01 -07001487 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001488
1489 GrGLfloat r, g, b, a;
1490 static const GrGLfloat scale255 = 1.f / 255.f;
1491 a = GrColorUnpackA(color) * scale255;
1492 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001493 r = GrColorUnpackR(color) * scaleRGB;
1494 g = GrColorUnpackG(color) * scaleRGB;
1495 b = GrColorUnpackB(color) * scaleRGB;
1496
1497 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001498 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001499 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001500 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001501}
1502
bsalomon861e1032014-12-16 07:33:49 -08001503void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001504 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001505 if (!this->caps()->discardRenderTargetSupport()) {
1506 return;
1507 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001508
1509 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon1c63bf62014-07-22 13:09:46 -07001510 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1511 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
robertphillips754f4e92014-09-18 13:52:08 -07001512 fGPUStats.incRenderTargetBinds();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001513 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1514 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001515 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001516 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001517 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001518 break;
1519 case GrGLCaps::kInvalidate_InvalidateFBType:
1520 if (0 == glRT->renderFBOID()) {
1521 // When rendering to the default framebuffer the legal values for attachments
1522 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1523 // types.
1524 static const GrGLenum attachments[] = { GR_GL_COLOR };
1525 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1526 attachments));
1527 } else {
1528 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1529 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1530 attachments));
1531 }
1532 break;
1533 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001534 if (0 == glRT->renderFBOID()) {
1535 // When rendering to the default framebuffer the legal values for attachments
1536 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1537 // types. See glDiscardFramebuffer() spec.
1538 static const GrGLenum attachments[] = { GR_GL_COLOR };
1539 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1540 attachments));
1541 } else {
1542 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1543 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1544 attachments));
1545 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001546 break;
1547 }
1548 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001549 renderTarget->flagAsResolved();
1550}
1551
1552
bsalomon861e1032014-12-16 07:33:49 -08001553void GrGLGpu::clearStencil(GrRenderTarget* target) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001554 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001555 return;
1556 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001557 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1558 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001559
joshualitt77b13072014-10-27 14:51:01 -07001560 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001561
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001562 GL_CALL(StencilMask(0xffffffff));
1563 GL_CALL(ClearStencil(0));
1564 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001565 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001566}
1567
bsalomon861e1032014-12-16 07:33:49 -08001568void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001569 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001570
1571 // this should only be called internally when we know we have a
1572 // stencil buffer.
bsalomon49f085d2014-09-05 13:34:00 -07001573 SkASSERT(target->getStencilBuffer());
bsalomonb0bd4f62014-09-03 07:19:50 -07001574 GrGLint stencilBitCount = target->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001575#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001576 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001577 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001578#else
1579 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001580 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001581 // turned into draws. Our contract on GrDrawTarget says that
1582 // changing the clip between stencil passes may or may not
1583 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001584 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001585#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001586 GrGLint value;
1587 if (insideClip) {
1588 value = (1 << (stencilBitCount - 1));
1589 } else {
1590 value = 0;
1591 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001592 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1593 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001594
bsalomon3e791242014-12-17 13:43:13 -08001595 GrScissorState scissorState;
joshualitt77b13072014-10-27 14:51:01 -07001596 scissorState.fEnabled = true;
1597 scissorState.fRect = rect;
1598 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001599
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001600 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001601 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001602 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001603 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001604}
1605
bsalomon861e1032014-12-16 07:33:49 -08001606bool GrGLGpu::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001607 int left, int top,
1608 int width, int height,
1609 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001610 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001611 // If this rendertarget is aready TopLeft, we don't need to flip.
1612 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1613 return false;
1614 }
1615
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001616 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001617 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001618 return false;
1619 }
1620
1621 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001622 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001623 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001624 return true;
1625 }
1626 // If we have to do memcpys to handle rowBytes then y-flip is free
1627 // Note the rowBytes might be tight to the passed in data, but if data
1628 // gets clipped in x to the target the rowBytes will no longer be tight.
1629 if (left >= 0 && (left + width) < renderTarget->width()) {
1630 return 0 == rowBytes ||
1631 GrBytesPerPixel(config) * width == rowBytes;
1632 } else {
1633 return false;
1634 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001635}
1636
bsalomon861e1032014-12-16 07:33:49 -08001637bool GrGLGpu::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001638 int left, int top,
1639 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001640 GrPixelConfig config,
1641 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001642 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001643 // We cannot read pixels into a compressed buffer
1644 if (GrPixelConfigIsCompressed(config)) {
1645 return false;
1646 }
1647
1648 GrGLenum format = 0;
1649 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001650 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001651 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001653 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001654 size_t bpp = GrBytesPerPixel(config);
1655 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1656 &left, &top, &width, &height,
1657 const_cast<const void**>(&buffer),
1658 &rowBytes)) {
1659 return false;
1660 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001661
bsalomon@google.comc6980972011-11-02 19:57:21 +00001662 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001663 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001664 switch (tgt->getResolveType()) {
1665 case GrGLRenderTarget::kCantResolve_ResolveType:
1666 return false;
1667 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon37dd3312014-11-03 08:47:23 -08001668 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001669 break;
1670 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001671 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001672 // we don't track the state of the READ FBO ID.
robertphillips754f4e92014-09-18 13:52:08 -07001673 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001674 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1675 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001676 break;
1677 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001678 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001679 }
1680
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001681 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001682
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001683 // the read rect is viewport-relative
1684 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001685 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001686
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001687 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001688 if (0 == rowBytes) {
1689 rowBytes = tightRowBytes;
1690 }
1691 size_t readDstRowBytes = tightRowBytes;
1692 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001693
bsalomon@google.comc6980972011-11-02 19:57:21 +00001694 // determine if GL can read using the passed rowBytes or if we need
1695 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001696 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001697 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001698 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001699 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001700 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001701 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001702 readDstRowBytes = rowBytes;
1703 } else {
1704 scratch.reset(tightRowBytes * height);
1705 readDst = scratch.get();
1706 }
1707 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001708 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001709 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1710 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001711 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1712 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001713 format, type, readDst));
1714 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001715 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001716 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1717 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001718 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001719 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001720 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001721 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001722
1723 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001724 // API presents top-to-bottom. We must preserve the padding contents. Note
1725 // that the above readPixels did not overwrite the padding.
1726 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001727 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001728 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001729 scratch.reset(tightRowBytes);
1730 void* tmpRow = scratch.get();
1731 // flip y in-place by rows
1732 const int halfY = height >> 1;
1733 char* top = reinterpret_cast<char*>(buffer);
1734 char* bottom = top + (height - 1) * rowBytes;
1735 for (int y = 0; y < halfY; y++) {
1736 memcpy(tmpRow, top, tightRowBytes);
1737 memcpy(top, bottom, tightRowBytes);
1738 memcpy(bottom, tmpRow, tightRowBytes);
1739 top += rowBytes;
1740 bottom -= rowBytes;
1741 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001742 }
1743 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001744 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001745 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001746 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001747 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001748 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001749 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001750 dst += (height-1) * rowBytes;
1751 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001752 for (int y = 0; y < height; y++) {
1753 memcpy(dst, src, tightRowBytes);
1754 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001755 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001756 dst += rowBytes;
1757 } else {
1758 dst -= rowBytes;
1759 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 }
1761 }
1762 return true;
1763}
1764
bsalomon861e1032014-12-16 07:33:49 -08001765void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001766
bsalomon49f085d2014-09-05 13:34:00 -07001767 SkASSERT(target);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001768
bsalomonb0bd4f62014-09-03 07:19:50 -07001769 uint32_t rtID = target->getUniqueID();
bsalomon1c63bf62014-07-22 13:09:46 -07001770 if (fHWBoundRenderTargetUniqueID != rtID) {
robertphillips754f4e92014-09-18 13:52:08 -07001771 fGPUStats.incRenderTargetBinds();
bsalomonb0bd4f62014-09-03 07:19:50 -07001772 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001773#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001774 // don't do this check in Chromium -- this is causing
1775 // lots of repeated command buffer flushes when the compositor is
1776 // rendering with Ganesh, which is really slow; even too slow for
1777 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001778 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001779 GrGLenum status;
1780 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1781 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon861e1032014-12-16 07:33:49 -08001782 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
rmistry@google.comd6bab022013-12-02 13:50:38 +00001783 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001785#endif
bsalomon1c63bf62014-07-22 13:09:46 -07001786 fHWBoundRenderTargetUniqueID = rtID;
bsalomonb0bd4f62014-09-03 07:19:50 -07001787 const GrGLIRect& vp = target->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001788 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001790 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001791 }
1792 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001793 if (NULL == bound || !bound->isEmpty()) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001794 target->flagAsNeedingResolve(bound);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001795 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001796
bsalomonb0bd4f62014-09-03 07:19:50 -07001797 GrTexture *texture = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001798 if (texture) {
bsalomonafbf2d62014-09-30 12:18:44 -07001799 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001800 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001801}
1802
twiz@google.com0f31ca72011-03-18 17:38:11 +00001803GrGLenum gPrimitiveType2GLMode[] = {
1804 GR_GL_TRIANGLES,
1805 GR_GL_TRIANGLE_STRIP,
1806 GR_GL_TRIANGLE_FAN,
1807 GR_GL_POINTS,
1808 GR_GL_LINES,
1809 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001810};
1811
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812#define SWAP_PER_DRAW 0
1813
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001814#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001815 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001817 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001818 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819 void SwapBuf() {
1820 DWORD procID = GetCurrentProcessId();
1821 HWND hwnd = GetTopWindow(GetDesktopWindow());
1822 while(hwnd) {
1823 DWORD wndProcID = 0;
1824 GetWindowThreadProcessId(hwnd, &wndProcID);
1825 if(wndProcID == procID) {
1826 SwapBuffers(GetDC(hwnd));
1827 }
1828 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1829 }
1830 }
1831 #endif
1832#endif
1833
bsalomon861e1032014-12-16 07:33:49 -08001834void GrGLGpu::onDraw(const GrOptDrawState& ds, const GrDrawTarget::DrawInfo& info) {
bsalomond95263c2014-12-16 13:05:12 -08001835 if (!this->flushGLState(ds)) {
1836 return;
1837 }
1838
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001839 size_t indexOffsetInBytes;
joshualittd53a8272014-11-10 16:03:14 -08001840 this->setupGeometry(ds, info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001841
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001842 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001843
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001844 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001845 GrGLvoid* indices =
1846 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001847 // info.startVertex() was accounted for by setupGeometry.
1848 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1849 info.indexCount(),
1850 GR_GL_UNSIGNED_SHORT,
1851 indices));
1852 } else {
1853 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1854 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1855 // accounted for startVertex.
1856 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1857 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858#if SWAP_PER_DRAW
1859 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001860 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861 aglSwapBuffers(aglGetCurrentContext());
1862 int set_a_break_pt_here = 9;
1863 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001864 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865 SwapBuf();
1866 int set_a_break_pt_here = 9;
1867 SwapBuf();
1868 #endif
1869#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001870}
1871
bsalomon3e791242014-12-17 13:43:13 -08001872void GrGLGpu::onStencilPath(const GrPath* path, const StencilPathState& state) {
egdaniel080e6732014-12-22 07:35:52 -08001873 this->flushColorWrite(false);
bsalomon3e791242014-12-17 13:43:13 -08001874 this->flushDrawFace(GrDrawState::kBoth_DrawFace);
1875
1876 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(state.fRenderTarget);
bsalomon1f78c0a2014-12-17 09:43:13 -08001877 SkISize size = SkISize::Make(rt->width(), rt->height());
bsalomon3e791242014-12-17 13:43:13 -08001878 this->glPathRendering()->setProjectionMatrix(*state.fViewMatrix, size, rt->origin());
1879 this->flushScissor(*state.fScissor, rt->getViewport(), rt->origin());
1880 this->flushHWAAState(rt, state.fUseHWAA, false);
bsalomon1f78c0a2014-12-17 09:43:13 -08001881 this->flushRenderTarget(rt, NULL);
1882
bsalomon3e791242014-12-17 13:43:13 -08001883 fPathRendering->stencilPath(path, *state.fStencil);
bsalomond95263c2014-12-16 13:05:12 -08001884}
1885
1886void GrGLGpu::onDrawPath(const GrOptDrawState& ds, const GrPath* path,
1887 const GrStencilSettings& stencil) {
1888 if (!this->flushGLState(ds)) {
1889 return;
1890 }
1891 fPathRendering->drawPath(path, stencil);
1892}
1893
1894void GrGLGpu::onDrawPaths(const GrOptDrawState& ds,
1895 const GrPathRange* pathRange,
1896 const void* indices,
1897 GrDrawTarget::PathIndexType indexType,
1898 const float transformValues[],
1899 GrDrawTarget::PathTransformType transformType,
1900 int count,
1901 const GrStencilSettings& stencil) {
1902 if (!this->flushGLState(ds)) {
1903 return;
1904 }
1905 fPathRendering->drawPaths(pathRange, indices, indexType, transformValues,
1906 transformType, count, stencil);
1907}
1908
bsalomon861e1032014-12-16 07:33:49 -08001909void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001910 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001911 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001912 // Some extensions automatically resolves the texture when it is read.
1913 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001914 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
robertphillips754f4e92014-09-18 13:52:08 -07001915 fGPUStats.incRenderTargetBinds();
1916 fGPUStats.incRenderTargetBinds();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001917 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1918 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1919 // make sure we go through flushRenderTarget() since we've modified
1920 // the bound DRAW FBO ID.
bsalomon1c63bf62014-07-22 13:09:46 -07001921 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001922 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001923 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001924 GrGLIRect r;
1925 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1926 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001927
bsalomon@google.com347c3822013-05-01 20:10:01 +00001928 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001929 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08001930 GrScissorState scissorState;
joshualitt77b13072014-10-27 14:51:01 -07001931 scissorState.fEnabled = true;
1932 scissorState.fRect = dirtyRect;
1933 this->flushScissor(scissorState, rt->getViewport(), rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001934 GL_CALL(ResolveMultisampleFramebuffer());
1935 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001936 int right = r.fLeft + r.fWidth;
1937 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001938
1939 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07001940 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001941 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1942 r.fLeft, r.fBottom, right, top,
1943 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001944 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001945 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001946 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001947 }
1948}
1949
bsalomon@google.com411dad02012-06-05 20:24:20 +00001950namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001951
bsalomon@google.com411dad02012-06-05 20:24:20 +00001952
1953GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1954 static const GrGLenum gTable[] = {
1955 GR_GL_KEEP, // kKeep_StencilOp
1956 GR_GL_REPLACE, // kReplace_StencilOp
1957 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1958 GR_GL_INCR, // kIncClamp_StencilOp
1959 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1960 GR_GL_DECR, // kDecClamp_StencilOp
1961 GR_GL_ZERO, // kZero_StencilOp
1962 GR_GL_INVERT, // kInvert_StencilOp
1963 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001964 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001965 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1966 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1967 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1968 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1969 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1970 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1971 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1972 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001973 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001974 return gTable[op];
1975}
1976
1977void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001978 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001979 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001980 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07001981 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001982 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1983 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1984
1985 GrGLint ref = settings.funcRef(grFace);
1986 GrGLint mask = settings.funcMask(grFace);
1987 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001988
1989 if (GR_GL_FRONT_AND_BACK == glFace) {
1990 // we call the combined func just in case separate stencil is not
1991 // supported.
1992 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1993 GR_GL_CALL(gl, StencilMask(writeMask));
1994 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1995 } else {
1996 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1997 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1998 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1999 }
2000}
2001}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002002
bsalomon3e791242014-12-17 13:43:13 -08002003void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2004 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002005 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002006 if (kNo_TriState != fHWStencilTestEnabled) {
2007 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2008 fHWStencilTestEnabled = kNo_TriState;
2009 }
2010 } else {
2011 if (kYes_TriState != fHWStencilTestEnabled) {
2012 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2013 fHWStencilTestEnabled = kYes_TriState;
2014 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002015 }
joshualitta58fe352014-10-27 08:39:00 -07002016 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002017 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002018 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002019 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002020 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002021 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002022 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002023 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002024 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002025 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002026 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002027 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002028 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002029 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002030 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002031 }
2032 }
joshualitta58fe352014-10-27 08:39:00 -07002033 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002034 }
2035}
2036
bsalomon3e791242014-12-17 13:43:13 -08002037void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool isLineDraw) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00002038// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
2039// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
2040#if 0
2041 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
2042 #define RT_HAS_MSAA rt->isMultisampled()
2043#else
bsalomon3e791242014-12-17 13:43:13 -08002044 #define RT_HAS_MSAA (rt->isMultisampled() || isLineDraw)
bsalomon@google.com202d1392013-03-19 18:58:08 +00002045#endif
2046
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002047 if (kGL_GrGLStandard == this->glStandard()) {
egdanielb414f252014-07-29 13:15:47 -07002048 if (RT_HAS_MSAA) {
bsalomon3e791242014-12-17 13:43:13 -08002049 if (useHWAA) {
egdanielb414f252014-07-29 13:15:47 -07002050 if (kYes_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002051 GL_CALL(Enable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002052 fMSAAEnabled = kYes_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002053 }
2054 } else {
egdanielb414f252014-07-29 13:15:47 -07002055 if (kNo_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002056 GL_CALL(Disable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002057 fMSAAEnabled = kNo_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002058 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002059 }
2060 }
2061 }
2062}
2063
egdaniel080e6732014-12-22 07:35:52 -08002064void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002065 // Any optimization to disable blending should have already been applied and
2066 // tweaked the coeffs to (1, 0).
egdanielc2304142014-12-11 13:15:13 -08002067
egdanielc2304142014-12-11 13:15:13 -08002068 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2069 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
egdanielb414f252014-07-29 13:15:47 -07002070 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2071 if (blendOff) {
2072 if (kNo_TriState != fHWBlendState.fEnabled) {
2073 GL_CALL(Disable(GR_GL_BLEND));
2074 fHWBlendState.fEnabled = kNo_TriState;
2075 }
2076 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002077 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002078 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002079 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002080 }
egdanielb414f252014-07-29 13:15:47 -07002081 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2082 fHWBlendState.fDstCoeff != dstCoeff) {
2083 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2084 gXfermodeCoeff2Blend[dstCoeff]));
2085 fHWBlendState.fSrcCoeff = srcCoeff;
2086 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002087 }
egdanielc2304142014-12-11 13:15:13 -08002088 GrColor blendConst = blendInfo.fBlendConstant;
egdanielb414f252014-07-29 13:15:47 -07002089 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2090 BlendCoeffReferencesConstant(dstCoeff)) &&
2091 (!fHWBlendState.fConstColorValid ||
2092 fHWBlendState.fConstColor != blendConst)) {
2093 GrGLfloat c[4];
2094 GrColorToRGBAFloat(blendConst, c);
2095 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2096 fHWBlendState.fConstColor = blendConst;
2097 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002098 }
2099 }
2100}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002101
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002102static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002103 static const GrGLenum gWrapModes[] = {
2104 GR_GL_CLAMP_TO_EDGE,
2105 GR_GL_REPEAT,
2106 GR_GL_MIRRORED_REPEAT
2107 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002108 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002109 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2110 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2111 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2112 return gWrapModes[tm];
2113}
2114
bsalomon861e1032014-12-16 07:33:49 -08002115void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002116 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002117
bsalomon@google.comb8670992012-07-25 21:27:09 +00002118 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2119 // 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 +00002120 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002121 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002122 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002123 this->onResolveRenderTarget(texRT);
2124 }
2125
bsalomon1c63bf62014-07-22 13:09:46 -07002126 uint32_t textureID = texture->getUniqueID();
2127 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002128 this->setTextureUnit(unitIdx);
2129 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002130 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002131 }
2132
bsalomon@google.com4c883782012-06-04 19:05:11 +00002133 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002134 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002135 bool setAll = timestamp < this->getResetTimestamp();
2136 GrGLTexture::TexParams newTexParams;
2137
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002138 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002139 GR_GL_NEAREST,
2140 GR_GL_LINEAR,
2141 GR_GL_LINEAR_MIPMAP_LINEAR
2142 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002143 static GrGLenum glMagFilterModes[] = {
2144 GR_GL_NEAREST,
2145 GR_GL_LINEAR,
2146 GR_GL_LINEAR
2147 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002148 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002149
2150 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2151 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2152 filterMode = GrTextureParams::kBilerp_FilterMode;
2153 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002154 }
bsalomonefd7d452014-10-23 14:17:46 -07002155
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002156 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2157 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002158
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002159 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002160 texture->texturePriv().mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002161 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002162 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002163 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002164
bsalomon@google.comb8670992012-07-25 21:27:09 +00002165 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2166 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002167 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002168 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002169 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002170 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002171 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002172 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2173 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002174 newTexParams.fMagFilter));
2175 }
2176 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2177 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002178 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2179 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002180 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002181 }
2182 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002183 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002184 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2185 GR_GL_TEXTURE_WRAP_S,
2186 newTexParams.fWrapS));
2187 }
2188 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002189 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002190 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2191 GR_GL_TEXTURE_WRAP_T,
2192 newTexParams.fWrapT));
2193 }
2194 if (this->glCaps().textureSwizzleSupport() &&
2195 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2196 oldTexParams.fSwizzleRGBA,
2197 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002198 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002199 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002200 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2201 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2202 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2203 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2204 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2205 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2206 } else {
2207 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2208 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2209 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2210 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002211 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002212 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002213}
2214
bsalomon3e791242014-12-17 13:43:13 -08002215void GrGLGpu::flushDither(bool dither) {
2216 if (dither) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002217 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002218 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002219 fHWDitherEnabled = kYes_TriState;
2220 }
2221 } else {
2222 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002223 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002224 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 }
2226 }
bsalomon3e791242014-12-17 13:43:13 -08002227}
reed@google.comac10a2d2010-12-22 21:39:39 +00002228
egdaniel080e6732014-12-22 07:35:52 -08002229void GrGLGpu::flushColorWrite(bool writeColor) {
2230 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002231 if (kNo_TriState != fHWWriteToColor) {
2232 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2233 GR_GL_FALSE, GR_GL_FALSE));
2234 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002235 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002236 } else {
2237 if (kYes_TriState != fHWWriteToColor) {
2238 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2239 fHWWriteToColor = kYes_TriState;
2240 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002241 }
bsalomon3e791242014-12-17 13:43:13 -08002242}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002243
bsalomon3e791242014-12-17 13:43:13 -08002244void GrGLGpu::flushDrawFace(GrDrawState::DrawFace face) {
2245 if (fHWDrawFace != face) {
2246 switch (face) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002247 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002248 GL_CALL(Enable(GR_GL_CULL_FACE));
2249 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002250 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002251 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002252 GL_CALL(Enable(GR_GL_CULL_FACE));
2253 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002255 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002256 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002257 break;
2258 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002259 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002260 }
bsalomon3e791242014-12-17 13:43:13 -08002261 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002263}
2264
bsalomon861e1032014-12-16 07:33:49 -08002265bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002266 bool getSizedInternalFormat,
2267 GrGLenum* internalFormat,
2268 GrGLenum* externalFormat,
2269 GrGLenum* externalType) {
2270 GrGLenum dontCare;
2271 if (NULL == internalFormat) {
2272 internalFormat = &dontCare;
2273 }
2274 if (NULL == externalFormat) {
2275 externalFormat = &dontCare;
2276 }
2277 if (NULL == externalType) {
2278 externalType = &dontCare;
2279 }
2280
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002281 if(!this->glCaps().isConfigTexturable(config)) {
2282 return false;
2283 }
2284
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002286 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002287 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002288 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002289 if (getSizedInternalFormat) {
2290 *internalFormat = GR_GL_RGBA8;
2291 } else {
2292 *internalFormat = GR_GL_RGBA;
2293 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002294 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002295 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002296 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002297 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002298 if (getSizedInternalFormat) {
2299 *internalFormat = GR_GL_BGRA8;
2300 } else {
2301 *internalFormat = GR_GL_BGRA;
2302 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002303 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002304 if (getSizedInternalFormat) {
2305 *internalFormat = GR_GL_RGBA8;
2306 } else {
2307 *internalFormat = GR_GL_RGBA;
2308 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002309 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002310 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002311 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002312 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002313 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002314 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002315 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002316 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002317 if (!this->glCaps().ES2CompatibilitySupport()) {
2318 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002319 } else {
2320 *internalFormat = GR_GL_RGB565;
2321 }
2322 } else {
2323 *internalFormat = GR_GL_RGB;
2324 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002325 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002326 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002327 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002328 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002329 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002330 if (getSizedInternalFormat) {
2331 *internalFormat = GR_GL_RGBA4;
2332 } else {
2333 *internalFormat = GR_GL_RGBA;
2334 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002335 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002336 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002337 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002338 // no sized/unsized internal format distinction here
2339 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002340 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002341 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002342 if (this->glCaps().textureRedSupport()) {
2343 *internalFormat = GR_GL_RED;
2344 *externalFormat = GR_GL_RED;
2345 if (getSizedInternalFormat) {
2346 *internalFormat = GR_GL_R8;
2347 } else {
2348 *internalFormat = GR_GL_RED;
2349 }
2350 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002351 } else {
2352 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002353 *externalFormat = GR_GL_ALPHA;
2354 if (getSizedInternalFormat) {
2355 *internalFormat = GR_GL_ALPHA8;
2356 } else {
2357 *internalFormat = GR_GL_ALPHA;
2358 }
2359 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002360 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002362 case kETC1_GrPixelConfig:
2363 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2364 break;
2365 case kLATC_GrPixelConfig:
2366 switch(this->glCaps().latcAlias()) {
2367 case GrGLCaps::kLATC_LATCAlias:
2368 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2369 break;
2370 case GrGLCaps::kRGTC_LATCAlias:
2371 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2372 break;
2373 case GrGLCaps::k3DC_LATCAlias:
2374 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2375 break;
2376 }
2377 break;
krajcevski238b4562014-06-30 09:09:22 -07002378 case kR11_EAC_GrPixelConfig:
2379 *internalFormat = GR_GL_COMPRESSED_R11;
2380 break;
krajcevski7ef21622014-07-16 15:21:13 -07002381
2382 case kASTC_12x12_GrPixelConfig:
2383 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2384 break;
2385
joshualittee5da552014-07-16 13:32:56 -07002386 case kRGBA_float_GrPixelConfig:
2387 *internalFormat = GR_GL_RGBA32F;
2388 *externalFormat = GR_GL_RGBA;
2389 *externalType = GR_GL_FLOAT;
2390 break;
krajcevski7ef21622014-07-16 15:21:13 -07002391
jvanverth28f9c602014-12-05 13:06:35 -08002392 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002393 if (this->glCaps().textureRedSupport()) {
2394 if (getSizedInternalFormat) {
2395 *internalFormat = GR_GL_R16F;
2396 } else {
2397 *internalFormat = GR_GL_RED;
2398 }
jvanverth28f9c602014-12-05 13:06:35 -08002399 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002400 } else {
2401 if (getSizedInternalFormat) {
2402 *internalFormat = GR_GL_ALPHA16F;
2403 } else {
2404 *internalFormat = GR_GL_ALPHA;
2405 }
2406 *externalFormat = GR_GL_ALPHA;
2407 }
2408 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002409 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002410 } else {
jvanverth1334c212014-12-18 05:44:55 -08002411 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002412 }
2413 break;
2414
reed@google.comac10a2d2010-12-22 21:39:39 +00002415 default:
2416 return false;
2417 }
2418 return true;
2419}
2420
bsalomon861e1032014-12-16 07:33:49 -08002421void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002422 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002423 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002424 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002425 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002426 }
2427}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002428
bsalomon861e1032014-12-16 07:33:49 -08002429void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002430 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002431 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002432 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2433 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2434 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002435 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002436 // clear out the this field so that if a program does use this unit it will rebind the correct
2437 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002438 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002439}
2440
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002441namespace {
2442// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002443inline bool can_blit_framebuffer(const GrSurface* dst,
2444 const GrSurface* src,
bsalomon861e1032014-12-16 07:33:49 -08002445 const GrGLGpu* gpu,
bsalomon@google.comeb851172013-04-15 13:51:00 +00002446 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002447 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2448 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002449 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002450 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2451 // or the rects are not the same (not just the same size but have the same edges).
2452 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2453 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2454 return false;
2455 }
bsalomon49f085d2014-09-05 13:34:00 -07002456 if (wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002457 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2458 }
2459 return true;
2460 } else {
2461 return false;
2462 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002463}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002464
2465inline bool can_copy_texsubimage(const GrSurface* dst,
2466 const GrSurface* src,
bsalomon861e1032014-12-16 07:33:49 -08002467 const GrGLGpu* gpu,
bsalomon@google.comeb851172013-04-15 13:51:00 +00002468 bool* wouldNeedTempFBO = NULL) {
2469 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2470 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2471 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002472 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002473 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2474 return false;
2475 }
2476 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2477 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2478 // then we don't want to copy to the texture but to the MSAA buffer.
bsalomon49f085d2014-09-05 13:34:00 -07002479 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002480 return false;
2481 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002482 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2483 // If the src is multisampled (and uses an extension where there is a separate MSAA
2484 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
bsalomon49f085d2014-09-05 13:34:00 -07002485 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002486 return false;
2487 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002488 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002489 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002490 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002491 !GrPixelConfigIsCompressed(src->config())) {
bsalomon49f085d2014-09-05 13:34:00 -07002492 if (wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002493 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2494 }
2495 return true;
2496 } else {
2497 return false;
2498 }
2499}
2500
robertphillips754f4e92014-09-18 13:52:08 -07002501}
2502
bsalomon@google.comeb851172013-04-15 13:51:00 +00002503// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2504// relative to is output.
bsalomon861e1032014-12-16 07:33:49 -08002505GrGLuint GrGLGpu::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002506 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2507 GrGLuint tempFBOID;
2508 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002509 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002510 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
robertphillips754f4e92014-09-18 13:52:08 -07002511 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, &tempFBOID));
2512 fGPUStats.incRenderTargetBinds();
2513 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, tempFBOID));
2514 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2515 GR_GL_COLOR_ATTACHMENT0,
2516 GR_GL_TEXTURE_2D,
2517 texID,
2518 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002519 viewport->fLeft = 0;
2520 viewport->fBottom = 0;
2521 viewport->fWidth = surface->width();
2522 viewport->fHeight = surface->height();
2523 } else {
2524 tempFBOID = 0;
robertphillips754f4e92014-09-18 13:52:08 -07002525 fGPUStats.incRenderTargetBinds();
2526 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002527 *viewport = rt->getViewport();
2528 }
2529 return tempFBOID;
2530}
2531
bsalomon861e1032014-12-16 07:33:49 -08002532bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
bsalomonf90a02b2014-11-26 12:28:00 -08002533 // In here we look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
2534 // possible and we return false to fallback to creating a render target dst for render-to-
2535 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2536 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2537
bsalomon@google.comeb851172013-04-15 13:51:00 +00002538 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002539 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002540 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002541 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2542 // then we set up for that, otherwise fail.
2543 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2544 desc->fOrigin = kDefault_GrSurfaceOrigin;
2545 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
2546 desc->fConfig = kBGRA_8888_GrPixelConfig;
2547 return true;
2548 }
2549 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002550 } else if (NULL == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002551 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2552 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002553 }
2554
2555 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002556 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002557 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2558 // fail.
2559 if (this->caps()->isConfigRenderable(src->config(), false)) {
2560 desc->fOrigin = kDefault_GrSurfaceOrigin;
2561 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
2562 desc->fConfig = src->config();
2563 return true;
2564 }
2565 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002566 }
bsalomonf90a02b2014-11-26 12:28:00 -08002567
2568 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2569 desc->fConfig = src->config();
2570 desc->fOrigin = src->origin();
2571 desc->fFlags = kNone_GrSurfaceFlags;
2572 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002573}
2574
bsalomon861e1032014-12-16 07:33:49 -08002575bool GrGLGpu::copySurface(GrSurface* dst,
joshualitta7024152014-11-03 14:16:35 -08002576 GrSurface* src,
2577 const SkIRect& srcRect,
2578 const SkIPoint& dstPoint) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002579 bool copied = false;
joshualitta7024152014-11-03 14:16:35 -08002580 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002581 GrGLuint srcFBO;
2582 GrGLIRect srcVP;
robertphillips754f4e92014-09-18 13:52:08 -07002583 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002584 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002585 SkASSERT(dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002586 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002587 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002588 GrGLIRect srcGLRect;
2589 srcGLRect.setRelativeTo(srcVP,
2590 srcRect.fLeft,
2591 srcRect.fTop,
2592 srcRect.width(),
2593 srcRect.height(),
2594 src->origin());
2595
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002596 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002597 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2598 GrGLint dstY;
2599 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2600 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2601 } else {
2602 dstY = dstPoint.fY;
2603 }
2604 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2605 dstPoint.fX, dstY,
2606 srcGLRect.fLeft, srcGLRect.fBottom,
2607 srcGLRect.fWidth, srcGLRect.fHeight));
2608 copied = true;
2609 if (srcFBO) {
2610 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2611 }
joshualitta7024152014-11-03 14:16:35 -08002612 } else if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002613 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2614 srcRect.width(), srcRect.height());
2615 bool selfOverlap = false;
bsalomona2c23232014-11-25 07:41:12 -08002616 if (dst == src) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002617 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2618 }
2619
2620 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002621 GrGLuint dstFBO;
2622 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002623 GrGLIRect dstVP;
2624 GrGLIRect srcVP;
robertphillips754f4e92014-09-18 13:52:08 -07002625 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2626 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002627 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002628 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002629 GrGLIRect srcGLRect;
2630 GrGLIRect dstGLRect;
2631 srcGLRect.setRelativeTo(srcVP,
2632 srcRect.fLeft,
2633 srcRect.fTop,
2634 srcRect.width(),
2635 srcRect.height(),
2636 src->origin());
2637 dstGLRect.setRelativeTo(dstVP,
2638 dstRect.fLeft,
2639 dstRect.fTop,
2640 dstRect.width(),
2641 dstRect.height(),
2642 dst->origin());
2643
derekf8c8f71a2014-09-16 06:24:57 -07002644 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002645 this->disableScissor();
derekf8c8f71a2014-09-16 06:24:57 -07002646
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002647 GrGLint srcY0;
2648 GrGLint srcY1;
2649 // Does the blit need to y-mirror or not?
2650 if (src->origin() == dst->origin()) {
2651 srcY0 = srcGLRect.fBottom;
2652 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2653 } else {
2654 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2655 srcY1 = srcGLRect.fBottom;
2656 }
2657 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2658 srcY0,
2659 srcGLRect.fLeft + srcGLRect.fWidth,
2660 srcY1,
2661 dstGLRect.fLeft,
2662 dstGLRect.fBottom,
2663 dstGLRect.fLeft + dstGLRect.fWidth,
2664 dstGLRect.fBottom + dstGLRect.fHeight,
2665 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2666 if (dstFBO) {
2667 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2668 }
2669 if (srcFBO) {
2670 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2671 }
2672 copied = true;
2673 }
2674 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002675 return copied;
2676}
2677
bsalomon861e1032014-12-16 07:33:49 -08002678bool GrGLGpu::canCopySurface(const GrSurface* dst,
joshualitt9853cce2014-11-17 14:22:48 -08002679 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -08002680 const SkIRect& srcRect,
2681 const SkIPoint& dstPoint) {
2682 // This mirrors the logic in onCopySurface. We prefer our base makes the copy if we need to
bsalomonf90a02b2014-11-26 12:28:00 -08002683 // create a temp fbo. TODO verify the assumption that temp fbos are expensive; it may not be
2684 // true at all.
joshualitta7024152014-11-03 14:16:35 -08002685 bool wouldNeedTempFBO = false;
2686 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) && !wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002687 return true;
2688 }
joshualitta7024152014-11-03 14:16:35 -08002689 if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) && !wouldNeedTempFBO) {
bsalomona2c23232014-11-25 07:41:12 -08002690 if (dst == src) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002691 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2692 srcRect.width(), srcRect.height());
2693 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2694 return true;
2695 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002696 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002697 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002698 }
2699 }
joshualitta7024152014-11-03 14:16:35 -08002700 return false;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002701}
2702
bsalomon861e1032014-12-16 07:33:49 -08002703void GrGLGpu::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002704 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002705 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002706 SkString markerString = markerArray.toStringLast();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002707 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002708 }
2709}
2710
bsalomon861e1032014-12-16 07:33:49 -08002711void GrGLGpu::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002712 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002713 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002714 }
2715}
joshualitt79f8fae2014-10-28 17:59:26 -07002716
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002717///////////////////////////////////////////////////////////////////////////////
2718
bsalomon861e1032014-12-16 07:33:49 -08002719GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
2720 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00002721 const GrGLVertexBuffer* vbuffer,
2722 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002723 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002724 GrGLAttribArrayState* attribState;
2725
bsalomon@google.com6918d482013-03-07 19:09:11 +00002726 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2727 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002728 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002729 SkSafeUnref(fVBOVertexArray);
2730 GrGLuint arrayID;
2731 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2732 int attrCount = gpu->glCaps().maxVertexAttributes();
2733 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002734 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002735 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2736 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002737 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002738 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2739 } else {
2740 this->setVertexArrayID(gpu, 0);
2741 }
2742 int attrCount = gpu->glCaps().maxVertexAttributes();
2743 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2744 fDefaultVertexArrayAttribState.resize(attrCount);
2745 }
2746 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002747 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002748 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002749}