• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "common/rs_shared_context.h"
17 
18 #include <EGL/egl.h>
19 #include "tools/gpu/GrContextFactory.h"
20 
21 #include "platform/common/rs_log.h"
22 #include "rs_trace.h"
23 
24 namespace OHOS::Rosen {
MakeSharedGLContext(EGLContext context)25 std::shared_ptr<RSSharedContext> RSSharedContext::MakeSharedGLContext(EGLContext context)
26 {
27     RS_TRACE_NAME_FMT("MakeSharedGLContext create egl ");
28     if (context == EGL_NO_CONTEXT) {
29         RS_LOGE("eglGetCurrentContext failed err:%d", eglGetError());
30         return nullptr;
31     }
32     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
33     if (display == EGL_NO_DISPLAY) {
34         RS_LOGE("eglGetDisplay failed err:%d", eglGetError());
35         return nullptr;
36     }
37     EGLint attributes[] = {
38         // clang-format off
39         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
40         EGL_SURFACE_TYPE,    EGL_WINDOW_BIT,
41         EGL_RED_SIZE,        8,
42         EGL_GREEN_SIZE,      8,
43         EGL_BLUE_SIZE,       8,
44         EGL_ALPHA_SIZE,      8,
45         EGL_DEPTH_SIZE,      0,
46         EGL_STENCIL_SIZE,    0,
47         EGL_NONE,            // termination sentinel
48         // clang-format on
49     };
50     EGLint config_count = 0;
51     EGLConfig egl_config = nullptr;
52     if (eglChooseConfig(display, attributes, &egl_config, 1, &config_count) != EGL_TRUE) {
53         RS_LOGE("Get EGLConfig failed err:%d", eglGetError());
54         return nullptr;
55     }
56     EGLint contextAttr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
57     EGLContext shareContext = eglCreateContext(display, egl_config, context, contextAttr);
58     if (shareContext == EGL_NO_CONTEXT) {
59         RS_LOGE("eglCreateContext failed err:%d", eglGetError());
60         return nullptr;
61     }
62     const EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
63     EGLSurface surface = eglCreatePbufferSurface(display, egl_config, attribs);
64     if (surface == EGL_NO_SURFACE) {
65         RS_LOGE("eglCreatePbufferSurface failed err:%d", eglGetError());
66         eglDestroyContext(display, context);
67         return nullptr;
68     }
69     RS_LOGI("create egl success");
70     return std::shared_ptr<RSSharedContext>(new RSSharedContext(display, shareContext, surface));
71 }
72 
~RSSharedContext()73 RSSharedContext::~RSSharedContext()
74 {
75     if (surface_ != EGL_NO_SURFACE) {
76         eglDestroySurface(display_, surface_);
77     }
78 
79     if (context_ != EGL_NO_CONTEXT) {
80         eglDestroyContext(display_, context_);
81     }
82 }
83 
MakeCurrent()84 void RSSharedContext::MakeCurrent()
85 {
86     if (eglMakeCurrent(display_, surface_, surface_, context_) != EGL_TRUE) {
87         RS_LOGE("eglMakeCurrent failed");
88     }
89 }
90 
MakeGrContext()91 sk_sp<GrContext> RSSharedContext::MakeGrContext()
92 {
93     GrContextOptions options;
94     options.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
95     options.fPreferExternalImagesOverES3 = true;
96     options.fDisableDistanceFieldPaths = true;
97     return GrContext::MakeGL(GrGLMakeNativeInterface(), options);
98 }
99 } // namespace OHOS::Rosen
100