• 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 "gles_render_backend.h"
17 
18 #include "egl_manager.h"
19 #include "drawing_utils.h"
20 #include "surface_ohos_gl.h"
21 #include "iostream"
22 
23 namespace OHOS {
24 namespace Rosen {
GLESRenderBackend()25 GLESRenderBackend::GLESRenderBackend() noexcept
26 {
27     eglManager_ = new EGLManager();
28 }
29 
~GLESRenderBackend()30 GLESRenderBackend::~GLESRenderBackend()
31 {
32     if (eglManager_) {
33         delete eglManager_;
34         eglManager_ = nullptr;
35     }
36 }
37 
InitDrawContext()38 void GLESRenderBackend::InitDrawContext()
39 {
40     if (eglManager_ == nullptr) {
41         LOGE("eglManager_ is nullptr, can not InitDrawContext");
42         return;
43     }
44     eglManager_->Init();
45 }
46 
SetUpGrContext()47 bool GLESRenderBackend::SetUpGrContext()
48 {
49     if (grContext_ != nullptr) {
50         LOGD("grContext has already created!!");
51         return true;
52     }
53 
54     sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
55     if (glInterface.get() == nullptr) {
56         LOGE("SetUpGrContext failed to make native interface");
57         return false;
58     }
59 
60     GrContextOptions options;
61     options.fPreferExternalImagesOverES3 = true;
62     options.fDisableDistanceFieldPaths = true;
63     options.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
64 #if defined(USE_CANVASKIT0310_SKIA) || defined(NEW_SKIA)
65     sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
66     if (grContext == nullptr) {
67         LOGE("SetUpGrContext grContext is null");
68         return false;
69     }
70 #else
71     sk_sp<GrContext> grContext(GrContext::MakeGL(std::move(glInterface), options));
72     if (grContext == nullptr) {
73         LOGE("SetUpGrContext grContext is null");
74         return false;
75     }
76 #endif
77     grContext_ = std::move(grContext);
78     LOGI("SetUpGrContext done");
79     return true;
80 }
81 
MakeCurrent()82 void GLESRenderBackend::MakeCurrent()
83 {
84     if (eglManager_ == nullptr) {
85         LOGE("eglManager_ is nullptr, can not MakeCurrent");
86         return;
87     }
88     eglManager_->MakeCurrent();
89 }
90 
SwapBuffers()91 void GLESRenderBackend::SwapBuffers()
92 {
93     if (eglManager_ == nullptr) {
94         LOGE("eglManager_ is nullptr, can not SwapBuffers");
95         return;
96     }
97     eglManager_->SwapBuffers();
98 }
99 
CreateSurface(void * window)100 void* GLESRenderBackend::CreateSurface(void* window)
101 {
102     if (eglManager_ == nullptr) {
103         LOGE("eglManager_ is nullptr, can not CreateSurface");
104         return nullptr;
105     }
106     return static_cast<void*>(eglManager_->CreateSurface((EGLNativeWindowType)window));
107 }
108 
SetDamageRegion(int32_t left,int32_t top,int32_t width,int32_t height)109 void GLESRenderBackend::SetDamageRegion(int32_t left, int32_t top, int32_t width, int32_t height)
110 {
111     if (eglManager_ == nullptr) {
112         LOGE("eglManager_ is nullptr, can not SetDamageRegion");
113         return;
114     }
115     eglManager_->SetDamageRegion(left, top, width, height);
116 }
117 
Destroy()118 void GLESRenderBackend::Destroy()
119 {
120     grContext_ = nullptr;
121     skSurface_ = nullptr;
122 }
123 
RenderFrame()124 void GLESRenderBackend::RenderFrame()
125 {
126     // flush commands
127     if (skSurface_->getCanvas() != nullptr) {
128         LOGD("RenderFrame: Canvas");
129         std::cout << "GLESRenderBackend::RenderFrame flushing" << std::endl;
130         skSurface_->getCanvas()->flush();
131     } else {
132         LOGW("canvas is nullptr!!!");
133     }
134 }
135 
AcquireCanvas(std::unique_ptr<SurfaceFrame> & frame)136 SkCanvas* GLESRenderBackend::AcquireCanvas(std::unique_ptr<SurfaceFrame>& frame)
137 {
138     if (!SetUpGrContext()) {
139         LOGE("GrContext is not ready!!!");
140         return nullptr;
141     }
142 
143     SurfaceFrameOhosGl* framePtr = static_cast<SurfaceFrameOhosGl*>(frame.get());
144 
145     if (GetGrContext() == nullptr) {
146         LOGE("GrContext is not ready!!!");
147         return nullptr;
148     }
149 
150     GrGLFramebufferInfo framebufferInfo;
151     framebufferInfo.fFBOID = 0;
152     framebufferInfo.fFormat = GL_RGBA8;
153 
154     SkColorType colorType = kRGBA_8888_SkColorType;
155 
156     int32_t width = framePtr->GetWidth();
157     int32_t height = framePtr->GetHeight();
158     const int stencilBufferSize = 8;
159 
160     GrBackendRenderTarget backendRenderTarget(width, height, 0, stencilBufferSize, framebufferInfo);
161 #if defined(USE_CANVASKIT0310_SKIA) || defined(NEW_SKIA)
162     SkSurfaceProps surfaceProps(0, kRGB_H_SkPixelGeometry);
163 #else
164     SkSurfaceProps surfaceProps = SkSurfaceProps::kLegacyFontHost_InitType;
165 #endif
166     skSurface_ = SkSurface::MakeFromBackendRenderTarget(
167         GetGrContext(), backendRenderTarget, kBottomLeft_GrSurfaceOrigin, colorType, nullptr, &surfaceProps);
168     if (skSurface_ == nullptr) {
169         LOGW("skSurface is nullptr");
170         return nullptr;
171     }
172 
173     LOGI("CreateCanvas successfully!!!");
174 
175     return skSurface_->getCanvas();
176 }
177 }
178 }