1/*
2 * Copyright (c) 2024 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, Hardware
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 "gtest/gtest.h"
17#include "EGL/egl.h"
18#include "EGL/eglext.h"
19#include "GLES3/gl32.h"
20#include "drawing_gpu_context.h"
21
22using namespace testing;
23using namespace testing::ext;
24
25namespace OHOS {
26namespace Rosen {
27namespace Drawing {
28class NativeDrawingGpuContextTest : public testing::Test {
29public:
30    static void SetUpTestCase();
31    static void TearDownTestCase();
32    void SetUp() override;
33    void TearDown() override;
34protected:
35    EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
36    EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
37    EGLContext eglContext_ = EGL_NO_CONTEXT;
38    EGLSurface eglSurface_ = EGL_NO_SURFACE;
39    OH_Drawing_GpuContext* gpuContext_ = nullptr;
40};
41
42void NativeDrawingGpuContextTest::SetUpTestCase() {}
43void NativeDrawingGpuContextTest::TearDownTestCase() {}
44void NativeDrawingGpuContextTest::SetUp()
45{
46    eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
47    EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
48
49    EGLint eglMajVers;
50    EGLint eglMinVers;
51    EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
52    EXPECT_EQ(ret, EGL_TRUE);
53
54    EGLint count;
55    EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
56        EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
57        EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
58    ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
59    EXPECT_EQ(ret, EGL_TRUE);
60    EXPECT_GE(count, 1);
61
62    const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
63    eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
64    EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
65
66    EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
67    eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
68    EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
69
70    ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
71    EXPECT_EQ(ret, EGL_TRUE);
72}
73
74void NativeDrawingGpuContextTest::TearDown()
75{
76    EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
77    EXPECT_EQ(ret, EGL_TRUE);
78
79    ret = eglDestroyContext(eglDisplay_, eglContext_);
80    EXPECT_EQ(ret, EGL_TRUE);
81
82    ret = eglTerminate(eglDisplay_);
83    EXPECT_EQ(ret, EGL_TRUE);
84
85    eglSurface_ = EGL_NO_SURFACE;
86    eglContext_ = EGL_NO_CONTEXT;
87    eglDisplay_ = EGL_NO_DISPLAY;
88}
89
90/*
91 * @tc.name  : NativeDrawingGpuContextTest_CreateFromGL
92 * @tc.desc  : test for CreateFromGL.
93 * @tc.size  : MediumTest
94 * @tc.type  : Function
95 * @tc.level : Level 1
96 */
97HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_CreateFromGL, TestSize.Level1)
98{
99    OH_Drawing_GpuContextOptions options;
100    options.allowPathMaskCaching = true;
101    gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
102    EXPECT_NE(gpuContext_, nullptr);
103    OH_Drawing_GpuContextDestroy(gpuContext_);
104
105    options.allowPathMaskCaching = false;
106    gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
107    EXPECT_NE(gpuContext_, nullptr);
108    OH_Drawing_GpuContextDestroy(gpuContext_);
109}
110} // namespace Drawing
111} // namespace Rosen
112} // namespace OHOS