xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gl/GrGLContext.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2013 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/gl/GrGLContext.h"
9*c8dee2aaSAndroid Build Coastguard Worker 
10*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/ganesh/GrContextOptions.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/gl/GrGLGLSL.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "src/sksl/SkSLGLSL.h"
13*c8dee2aaSAndroid Build Coastguard Worker 
14*c8dee2aaSAndroid Build Coastguard Worker #ifdef SK_BUILD_FOR_ANDROID
15*c8dee2aaSAndroid Build Coastguard Worker #include <sys/system_properties.h>
16*c8dee2aaSAndroid Build Coastguard Worker #endif
17*c8dee2aaSAndroid Build Coastguard Worker 
18*c8dee2aaSAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////////////////////
19*c8dee2aaSAndroid Build Coastguard Worker 
Make(sk_sp<const GrGLInterface> interface,const GrContextOptions & options)20*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<GrGLContext> GrGLContext::Make(sk_sp<const GrGLInterface> interface,
21*c8dee2aaSAndroid Build Coastguard Worker                                                const GrContextOptions& options) {
22*c8dee2aaSAndroid Build Coastguard Worker     if (!interface->validate()) {
23*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
24*c8dee2aaSAndroid Build Coastguard Worker     }
25*c8dee2aaSAndroid Build Coastguard Worker 
26*c8dee2aaSAndroid Build Coastguard Worker     ConstructorArgs args;
27*c8dee2aaSAndroid Build Coastguard Worker     args.fDriverInfo = GrGLGetDriverInfo(interface.get());
28*c8dee2aaSAndroid Build Coastguard Worker     if (args.fDriverInfo.fVersion == GR_GL_INVALID_VER) {
29*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
30*c8dee2aaSAndroid Build Coastguard Worker     }
31*c8dee2aaSAndroid Build Coastguard Worker 
32*c8dee2aaSAndroid Build Coastguard Worker     if (!GrGLGetGLSLGeneration(args.fDriverInfo, &args.fGLSLGeneration)) {
33*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
34*c8dee2aaSAndroid Build Coastguard Worker     }
35*c8dee2aaSAndroid Build Coastguard Worker 
36*c8dee2aaSAndroid Build Coastguard Worker     /*
37*c8dee2aaSAndroid Build Coastguard Worker      * Qualcomm drivers for the 3xx series have a horrendous bug with some drivers. Though they
38*c8dee2aaSAndroid Build Coastguard Worker      * claim to support GLES 3.00, some perfectly valid GLSL300 shaders will only compile with
39*c8dee2aaSAndroid Build Coastguard Worker      * #version 100, and will fail to compile with #version 300 es.  In the long term, we
40*c8dee2aaSAndroid Build Coastguard Worker      * need to lock this down to a specific driver version.
41*c8dee2aaSAndroid Build Coastguard Worker      * ?????/2019 - Qualcomm has fixed this for Android O+ devices (API 26+)
42*c8dee2aaSAndroid Build Coastguard Worker      * ?????/2015 - This bug is still present in Lollipop pre-mr1
43*c8dee2aaSAndroid Build Coastguard Worker      * 06/18/2015 - This bug does not affect the nexus 6 (which has an Adreno 4xx).
44*c8dee2aaSAndroid Build Coastguard Worker      */
45*c8dee2aaSAndroid Build Coastguard Worker #ifdef SK_BUILD_FOR_ANDROID
46*c8dee2aaSAndroid Build Coastguard Worker     if (!options.fDisableDriverCorrectnessWorkarounds &&
47*c8dee2aaSAndroid Build Coastguard Worker         args.fDriverInfo.fRenderer == GrGLRenderer::kAdreno3xx) {
48*c8dee2aaSAndroid Build Coastguard Worker         char androidAPIVersion[PROP_VALUE_MAX];
49*c8dee2aaSAndroid Build Coastguard Worker         int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersion);
50*c8dee2aaSAndroid Build Coastguard Worker         if (strLength == 0 || atoi(androidAPIVersion) < 26) {
51*c8dee2aaSAndroid Build Coastguard Worker             args.fGLSLGeneration = SkSL::GLSLGeneration::k100es;
52*c8dee2aaSAndroid Build Coastguard Worker         }
53*c8dee2aaSAndroid Build Coastguard Worker     }
54*c8dee2aaSAndroid Build Coastguard Worker #endif
55*c8dee2aaSAndroid Build Coastguard Worker 
56*c8dee2aaSAndroid Build Coastguard Worker     // Many ES3 drivers only advertise the ES2 image_external extension, but support the _essl3
57*c8dee2aaSAndroid Build Coastguard Worker     // extension, and require that it be enabled to work with ESSL3. Other devices require the ES2
58*c8dee2aaSAndroid Build Coastguard Worker     // extension to be enabled, even when using ESSL3. Some devices appear to only support the ES2
59*c8dee2aaSAndroid Build Coastguard Worker     // extension. As an extreme (optional) solution, we can fallback to using ES2 shading language
60*c8dee2aaSAndroid Build Coastguard Worker     // if we want to prioritize external texture support. skbug.com/7713
61*c8dee2aaSAndroid Build Coastguard Worker     if (GR_IS_GR_GL_ES(interface->fStandard) &&
62*c8dee2aaSAndroid Build Coastguard Worker         options.fPreferExternalImagesOverES3 &&
63*c8dee2aaSAndroid Build Coastguard Worker         !options.fDisableDriverCorrectnessWorkarounds &&
64*c8dee2aaSAndroid Build Coastguard Worker         interface->hasExtension("GL_OES_EGL_image_external") &&
65*c8dee2aaSAndroid Build Coastguard Worker         args.fGLSLGeneration >= SkSL::GLSLGeneration::k330 &&
66*c8dee2aaSAndroid Build Coastguard Worker         !interface->hasExtension("GL_OES_EGL_image_external_essl3") &&
67*c8dee2aaSAndroid Build Coastguard Worker         !interface->hasExtension("OES_EGL_image_external_essl3")) {
68*c8dee2aaSAndroid Build Coastguard Worker         args.fGLSLGeneration = SkSL::GLSLGeneration::k100es;
69*c8dee2aaSAndroid Build Coastguard Worker     }
70*c8dee2aaSAndroid Build Coastguard Worker 
71*c8dee2aaSAndroid Build Coastguard Worker     args.fContextOptions = &options;
72*c8dee2aaSAndroid Build Coastguard Worker     args.fInterface = std::move(interface);
73*c8dee2aaSAndroid Build Coastguard Worker 
74*c8dee2aaSAndroid Build Coastguard Worker     return std::unique_ptr<GrGLContext>(new GrGLContext(std::move(args)));
75*c8dee2aaSAndroid Build Coastguard Worker }
76*c8dee2aaSAndroid Build Coastguard Worker 
~GrGLContext()77*c8dee2aaSAndroid Build Coastguard Worker GrGLContext::~GrGLContext() {}
78*c8dee2aaSAndroid Build Coastguard Worker 
GrGLContextInfo(ConstructorArgs && args)79*c8dee2aaSAndroid Build Coastguard Worker GrGLContextInfo::GrGLContextInfo(ConstructorArgs&& args) {
80*c8dee2aaSAndroid Build Coastguard Worker     fInterface = std::move(args.fInterface);
81*c8dee2aaSAndroid Build Coastguard Worker     fDriverInfo = args.fDriverInfo;
82*c8dee2aaSAndroid Build Coastguard Worker     fGLSLGeneration = args.fGLSLGeneration;
83*c8dee2aaSAndroid Build Coastguard Worker 
84*c8dee2aaSAndroid Build Coastguard Worker     fGLCaps = sk_make_sp<GrGLCaps>(*args.fContextOptions, *this, fInterface.get());
85*c8dee2aaSAndroid Build Coastguard Worker }
86