blob: 08555168f7d818385b19ffc52110a876078d0877 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef EGLMANAGER_H
17#define EGLMANAGER_H
18
19#include <cutils/compiler.h>
20#include <EGL/egl.h>
John Reckd04794a2015-05-08 10:04:36 -070021#include <SkRect.h>
John Reck3b202512014-06-23 13:13:08 -070022#include <ui/GraphicBuffer.h>
23#include <utils/StrongPointer.h>
24
25namespace android {
26namespace uirenderer {
27namespace renderthread {
28
29class RenderThread;
30
31// This class contains the shared global EGL objects, such as EGLDisplay
32// and EGLConfig, which are re-used by CanvasContext
33class EglManager {
34public:
35 // Returns true on success, false on failure
36 void initialize();
37
38 bool hasEglContext();
39 void requireGlContext();
40
41 void usePBufferSurface();
42 EGLSurface createSurface(EGLNativeWindowType window);
43 void destroySurface(EGLSurface surface);
44
45 void destroy();
46
47 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
48 // Returns true if the current surface changed, false if it was already current
49 bool makeCurrent(EGLSurface surface);
50 void beginFrame(EGLSurface surface, EGLint* width, EGLint* height);
John Reckd04794a2015-05-08 10:04:36 -070051 bool swapBuffers(EGLSurface surface, const SkRect& dirty, EGLint width, EGLint height);
John Reck0e89e2b2014-10-31 14:49:06 -070052 void cancelFrame();
John Reck3b202512014-06-23 13:13:08 -070053
John Reck1125d1f2014-10-23 11:02:19 -070054 // Returns true iff the surface is now preserving buffers.
55 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070056
57 void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
58
John Reck55156372015-01-21 07:46:37 -080059 void fence();
60
John Reck3b202512014-06-23 13:13:08 -070061private:
62 friend class RenderThread;
63
64 EglManager(RenderThread& thread);
65 // EglContext is never destroyed, method is purposely not implemented
66 ~EglManager();
67
68 void loadConfig();
69 void createContext();
70 void initAtlas();
71
72 RenderThread& mRenderThread;
73
74 EGLDisplay mEglDisplay;
75 EGLConfig mEglConfig;
76 EGLContext mEglContext;
77 EGLSurface mPBufferSurface;
78
John Reck1125d1f2014-10-23 11:02:19 -070079 const bool mAllowPreserveBuffer;
80 bool mCanSetPreserveBuffer;
John Reck3b202512014-06-23 13:13:08 -070081
82 EGLSurface mCurrentSurface;
83
84 sp<GraphicBuffer> mAtlasBuffer;
85 int64_t* mAtlasMap;
86 size_t mAtlasMapSize;
John Reck0e89e2b2014-10-31 14:49:06 -070087
88 // Whether or not we are in the middle of drawing a frame. This is used
89 // to avoid switching surfaces mid-frame if requireGlContext() is called
90 // TODO: Need to be better about surface/context management so that this isn't
91 // necessary
92 bool mInFrame;
John Reck3b202512014-06-23 13:13:08 -070093};
94
95} /* namespace renderthread */
96} /* namespace uirenderer */
97} /* namespace android */
98
99#endif /* EGLMANAGER_H */