blob: 14a40704f6f9a17555b2764de8b8ce7dd87421cd [file] [log] [blame]
robertphillips@google.com6177e692013-02-28 20:16:25 +00001/*
2 * Copyright 2013 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.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gl/GrGLContext.h"
9#include "src/gpu/gl/GrGLGLSL.h"
10#include "src/sksl/SkSLCompiler.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +000011
Derek Sollenberger11b34d92019-06-17 09:19:21 -040012#ifdef SK_BUILD_FOR_ANDROID
13#include <sys/system_properties.h>
14#endif
15
robertphillips@google.com6177e692013-02-28 20:16:25 +000016////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +000017
Brian Salomon8ab1cc42017-12-07 12:40:00 -050018std::unique_ptr<GrGLContext> GrGLContext::Make(sk_sp<const GrGLInterface> interface,
19 const GrContextOptions& options) {
bsalomon424cc262015-05-22 10:37:30 -070020 if (!interface->validate()) {
halcanary96fcdcc2015-08-27 07:41:13 -070021 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -070022 }
23
Brian Salomon8ab1cc42017-12-07 12:40:00 -050024 const GrGLubyte* verUByte;
25 GR_GL_CALL_RET(interface.get(), verUByte, GetString(GR_GL_VERSION));
26 const char* ver = reinterpret_cast<const char*>(verUByte);
27
28 const GrGLubyte* rendererUByte;
29 GR_GL_CALL_RET(interface.get(), rendererUByte, GetString(GR_GL_RENDERER));
30 const char* renderer = reinterpret_cast<const char*>(rendererUByte);
31
32 ConstructorArgs args;
bsalomon424cc262015-05-22 10:37:30 -070033 args.fGLVersion = GrGLGetVersionFromString(ver);
34 if (GR_GL_INVALID_VER == args.fGLVersion) {
halcanary96fcdcc2015-08-27 07:41:13 -070035 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -070036 }
37
Brian Salomon8ab1cc42017-12-07 12:40:00 -050038 if (!GrGLGetGLSLGeneration(interface.get(), &args.fGLSLGeneration)) {
halcanary96fcdcc2015-08-27 07:41:13 -070039 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -070040 }
41
Brian Salomon8ab1cc42017-12-07 12:40:00 -050042 args.fVendor = GrGLGetVendor(interface.get());
bsalomon424cc262015-05-22 10:37:30 -070043
Brian Salomon4470e342018-04-04 14:27:48 -040044 args.fRenderer = GrGLGetRendererFromStrings(renderer, interface->fExtensions);
joshualitt55999962015-06-18 13:47:10 -070045
Brian Salomon266ef6d2017-09-22 11:27:42 -040046 GrGLGetANGLEInfoFromString(renderer, &args.fANGLEBackend, &args.fANGLEVendor,
47 &args.fANGLERenderer);
Derek Sollenberger11b34d92019-06-17 09:19:21 -040048
bsalomon424cc262015-05-22 10:37:30 -070049 /*
joshualitt55999962015-06-18 13:47:10 -070050 * Qualcomm drivers for the 3xx series have a horrendous bug with some drivers. Though they
51 * claim to support GLES 3.00, some perfectly valid GLSL300 shaders will only compile with
bsalomon424cc262015-05-22 10:37:30 -070052 * #version 100, and will fail to compile with #version 300 es. In the long term, we
53 * need to lock this down to a specific driver version.
Derek Sollenberger11b34d92019-06-17 09:19:21 -040054 * ?????/2019 - Qualcomm has fixed this for Android O+ devices (API 26+)
joshualitt55999962015-06-18 13:47:10 -070055 * ?????/2015 - This bug is still present in Lollipop pre-mr1
56 * 06/18/2015 - This bug does not affect the nexus 6 (which has an Adreno 4xx).
bsalomon424cc262015-05-22 10:37:30 -070057 */
Derek Sollenberger11b34d92019-06-17 09:19:21 -040058#ifdef SK_BUILD_FOR_ANDROID
59 if (!options.fDisableDriverCorrectnessWorkarounds &&
60 kAdreno3xx_GrGLRenderer == args.fRenderer) {
61 char androidAPIVersion[PROP_VALUE_MAX];
Derek Sollenberger91032f82019-06-21 14:57:42 -040062 int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersion);
Derek Sollenberger11b34d92019-06-17 09:19:21 -040063 if (strLength == 0 || atoi(androidAPIVersion) < 26) {
64 args.fGLSLGeneration = k110_GrGLSLGeneration;
65 }
bsalomon424cc262015-05-22 10:37:30 -070066 }
Derek Sollenberger11b34d92019-06-17 09:19:21 -040067#endif
bsalomon424cc262015-05-22 10:37:30 -070068
Brian Osman06d37462018-05-08 13:00:42 -040069 // Many ES3 drivers only advertise the ES2 image_external extension, but support the _essl3
70 // extension, and require that it be enabled to work with ESSL3. Other devices require the ES2
71 // extension to be enabled, even when using ESSL3. Some devices appear to only support the ES2
72 // extension. As an extreme (optional) solution, we can fallback to using ES2 shading language
73 // if we want to prioritize external texture support. skbug.com/7713
Kevin Lubick8aa203c2019-03-19 13:23:10 -040074 if (GR_IS_GR_GL_ES(interface->fStandard) &&
Brian Osman06d37462018-05-08 13:00:42 -040075 options.fPreferExternalImagesOverES3 &&
76 !options.fDisableDriverCorrectnessWorkarounds &&
77 interface->hasExtension("GL_OES_EGL_image_external") &&
78 args.fGLSLGeneration >= k330_GrGLSLGeneration &&
79 !interface->hasExtension("GL_OES_EGL_image_external_essl3") &&
80 !interface->hasExtension("OES_EGL_image_external_essl3")) {
81 args.fGLSLGeneration = k110_GrGLSLGeneration;
82 }
83
cdalton1acea862015-06-02 13:05:52 -070084 GrGLGetDriverInfo(interface->fStandard, args.fVendor, renderer, ver,
85 &args.fDriver, &args.fDriverVersion);
bsalomon682c2692015-05-22 14:01:46 -070086
87 args.fContextOptions = &options;
Brian Salomon8ab1cc42017-12-07 12:40:00 -050088 args.fInterface = std::move(interface);
bsalomon682c2692015-05-22 14:01:46 -070089
Brian Salomon8ab1cc42017-12-07 12:40:00 -050090 return std::unique_ptr<GrGLContext>(new GrGLContext(std::move(args)));
robertphillips@google.com6177e692013-02-28 20:16:25 +000091}
92
ethannicholas5961bc92016-10-12 06:39:56 -070093GrGLContext::~GrGLContext() {
94 delete fCompiler;
95}
96
97SkSL::Compiler* GrGLContext::compiler() const {
98 if (!fCompiler) {
99 fCompiler = new SkSL::Compiler();
100 }
101 return fCompiler;
102}
103
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500104GrGLContextInfo::GrGLContextInfo(ConstructorArgs&& args) {
105 fInterface = std::move(args.fInterface);
bsalomon424cc262015-05-22 10:37:30 -0700106 fGLVersion = args.fGLVersion;
107 fGLSLGeneration = args.fGLSLGeneration;
108 fVendor = args.fVendor;
109 fRenderer = args.fRenderer;
cdalton1acea862015-06-02 13:05:52 -0700110 fDriver = args.fDriver;
111 fDriverVersion = args.fDriverVersion;
Brian Salomon266ef6d2017-09-22 11:27:42 -0400112 fANGLEBackend = args.fANGLEBackend;
113 fANGLEVendor = args.fANGLEVendor;
114 fANGLERenderer = args.fANGLERenderer;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000115
Hal Canary144caf52016-11-07 17:57:18 -0500116 fGLCaps = sk_make_sp<GrGLCaps>(*args.fContextOptions, *this, fInterface.get());
robertphillips@google.com6177e692013-02-28 20:16:25 +0000117}