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 #ifdef ROSEN_IOS
19 #include "render_context/render_context_egl_defines.h"
20 #else
21 #include "EGL/egl.h"
22 #endif
23 #include "tools/gpu/GrContextFactory.h"
24
25 #include "platform/common/rs_log.h"
26 #include "rs_trace.h"
27
28 namespace OHOS::Rosen {
MakeSharedGLContext(EGLContext context)29 std::shared_ptr<RSSharedContext> RSSharedContext::MakeSharedGLContext(EGLContext context)
30 {
31 RS_TRACE_NAME_FMT("MakeSharedGLContext create egl ");
32 if (context == EGL_NO_CONTEXT) {
33 RS_LOGE("eglGetCurrentContext failed err:%d", eglGetError());
34 return nullptr;
35 }
36 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
37 if (display == EGL_NO_DISPLAY) {
38 RS_LOGE("eglGetDisplay failed err:%d", eglGetError());
39 return nullptr;
40 }
41 EGLint attributes[] = {
42 // clang-format off
43 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
44 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
45 EGL_RED_SIZE, 8,
46 EGL_GREEN_SIZE, 8,
47 EGL_BLUE_SIZE, 8,
48 EGL_ALPHA_SIZE, 8,
49 EGL_DEPTH_SIZE, 0,
50 EGL_STENCIL_SIZE, 0,
51 EGL_NONE, // termination sentinel
52 // clang-format on
53 };
54 EGLint config_count = 0;
55 EGLConfig egl_config = nullptr;
56 if (eglChooseConfig(display, attributes, &egl_config, 1, &config_count) != EGL_TRUE) {
57 RS_LOGE("Get EGLConfig failed err:%d", eglGetError());
58 return nullptr;
59 }
60 EGLint contextAttr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
61 EGLContext shareContext = eglCreateContext(display, egl_config, context, contextAttr);
62 if (shareContext == EGL_NO_CONTEXT) {
63 RS_LOGE("eglCreateContext failed err:%d", eglGetError());
64 return nullptr;
65 }
66 const EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
67 EGLSurface surface = eglCreatePbufferSurface(display, egl_config, attribs);
68 if (surface == EGL_NO_SURFACE) {
69 RS_LOGE("eglCreatePbufferSurface failed err:%d", eglGetError());
70 eglDestroyContext(display, context);
71 return nullptr;
72 }
73 RS_LOGI("create egl success");
74 return std::shared_ptr<RSSharedContext>(new RSSharedContext(display, shareContext, surface));
75 }
76
~RSSharedContext()77 RSSharedContext::~RSSharedContext()
78 {
79 if (surface_ != EGL_NO_SURFACE) {
80 eglDestroySurface(display_, surface_);
81 }
82
83 if (context_ != EGL_NO_CONTEXT) {
84 eglDestroyContext(display_, context_);
85 }
86 }
87
MakeCurrent()88 void RSSharedContext::MakeCurrent()
89 {
90 if (eglMakeCurrent(display_, surface_, surface_, context_) != EGL_TRUE) {
91 RS_LOGE("eglMakeCurrent failed");
92 }
93 }
94
95 #ifndef USE_ROSEN_DRAWING
96 #ifdef NEW_SKIA
MakeGrContext()97 sk_sp<GrDirectContext> RSSharedContext::MakeGrContext()
98 #else
99 sk_sp<GrContext> RSSharedContext::MakeGrContext()
100 #endif
101 {
102 GrContextOptions options;
103 options.fPreferExternalImagesOverES3 = true;
104 options.fDisableDistanceFieldPaths = true;
105 options.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
106 #ifdef NEW_SKIA
107 return GrDirectContext::MakeGL(GrGLMakeNativeInterface(), options);
108 #else
109 return GrContext::MakeGL(GrGLMakeNativeInterface(), options);
110 #endif
111 }
112 #else // USE_ROSEN_DRAWING
MakeDrContext()113 std::shared_ptr<Drawing::GPUContext> RSSharedContext::MakeDrContext()
114 {
115 Drawing::GPUContextOptions options;
116 std::shared_ptr<Drawing::GPUContext> GPUContext = std::make_shared<Drawing::GPUContext>();
117 GPUContext->BuildFromGL(options);
118 return GPUContext;
119 }
120 #endif
121 } // namespace OHOS::Rosen
122