13b20251a355c88193c439f928a84ae69483fb488John Reck/*
23b20251a355c88193c439f928a84ae69483fb488John Reck * Copyright (C) 2014 The Android Open Source Project
33b20251a355c88193c439f928a84ae69483fb488John Reck *
43b20251a355c88193c439f928a84ae69483fb488John Reck * Licensed under the Apache License, Version 2.0 (the "License");
53b20251a355c88193c439f928a84ae69483fb488John Reck * you may not use this file except in compliance with the License.
63b20251a355c88193c439f928a84ae69483fb488John Reck * You may obtain a copy of the License at
73b20251a355c88193c439f928a84ae69483fb488John Reck *
83b20251a355c88193c439f928a84ae69483fb488John Reck *      http://www.apache.org/licenses/LICENSE-2.0
93b20251a355c88193c439f928a84ae69483fb488John Reck *
103b20251a355c88193c439f928a84ae69483fb488John Reck * Unless required by applicable law or agreed to in writing, software
113b20251a355c88193c439f928a84ae69483fb488John Reck * distributed under the License is distributed on an "AS IS" BASIS,
123b20251a355c88193c439f928a84ae69483fb488John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133b20251a355c88193c439f928a84ae69483fb488John Reck * See the License for the specific language governing permissions and
143b20251a355c88193c439f928a84ae69483fb488John Reck * limitations under the License.
153b20251a355c88193c439f928a84ae69483fb488John Reck */
163b20251a355c88193c439f928a84ae69483fb488John Reck#ifndef EGLMANAGER_H
173b20251a355c88193c439f928a84ae69483fb488John Reck#define EGLMANAGER_H
183b20251a355c88193c439f928a84ae69483fb488John Reck
193b20251a355c88193c439f928a84ae69483fb488John Reck#include <cutils/compiler.h>
203b20251a355c88193c439f928a84ae69483fb488John Reck#include <EGL/egl.h>
21d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include <SkRect.h>
223b20251a355c88193c439f928a84ae69483fb488John Reck#include <ui/GraphicBuffer.h>
233b20251a355c88193c439f928a84ae69483fb488John Reck#include <utils/StrongPointer.h>
243b20251a355c88193c439f928a84ae69483fb488John Reck
253b20251a355c88193c439f928a84ae69483fb488John Recknamespace android {
263b20251a355c88193c439f928a84ae69483fb488John Recknamespace uirenderer {
273b20251a355c88193c439f928a84ae69483fb488John Recknamespace renderthread {
283b20251a355c88193c439f928a84ae69483fb488John Reck
29cd55852fcd840f7f4c4d7a0a7253a2995c77afa2Greg Danielclass Frame;
303b20251a355c88193c439f928a84ae69483fb488John Reckclass RenderThread;
313b20251a355c88193c439f928a84ae69483fb488John Reck
323b20251a355c88193c439f928a84ae69483fb488John Reck// This class contains the shared global EGL objects, such as EGLDisplay
333b20251a355c88193c439f928a84ae69483fb488John Reck// and EGLConfig, which are re-used by CanvasContext
343b20251a355c88193c439f928a84ae69483fb488John Reckclass EglManager {
353b20251a355c88193c439f928a84ae69483fb488John Reckpublic:
36694d499662838123f474f41b31dea84ec5d563f0sergeyv    static const char* eglErrorString();
373b20251a355c88193c439f928a84ae69483fb488John Reck    // Returns true on success, false on failure
383b20251a355c88193c439f928a84ae69483fb488John Reck    void initialize();
393b20251a355c88193c439f928a84ae69483fb488John Reck
403b20251a355c88193c439f928a84ae69483fb488John Reck    bool hasEglContext();
413b20251a355c88193c439f928a84ae69483fb488John Reck
423b20251a355c88193c439f928a84ae69483fb488John Reck    EGLSurface createSurface(EGLNativeWindowType window);
433b20251a355c88193c439f928a84ae69483fb488John Reck    void destroySurface(EGLSurface surface);
443b20251a355c88193c439f928a84ae69483fb488John Reck
453b20251a355c88193c439f928a84ae69483fb488John Reck    void destroy();
463b20251a355c88193c439f928a84ae69483fb488John Reck
473b20251a355c88193c439f928a84ae69483fb488John Reck    bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
483b20251a355c88193c439f928a84ae69483fb488John Reck    // Returns true if the current surface changed, false if it was already current
49f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
50149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    Frame beginFrame(EGLSurface surface);
51149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    void damageFrame(const Frame& frame, const SkRect& dirty);
52c96955d9bb997b51be5fa929b5a67349d0459c3aJohn Reck    // If this returns true it is mandatory that swapBuffers is called
53c96955d9bb997b51be5fa929b5a67349d0459c3aJohn Reck    // if damageFrame is called without subsequent calls to damageFrame().
54c96955d9bb997b51be5fa929b5a67349d0459c3aJohn Reck    // See EGL_KHR_partial_update for more information
55c96955d9bb997b51be5fa929b5a67349d0459c3aJohn Reck    bool damageRequiresSwap();
56149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
573b20251a355c88193c439f928a84ae69483fb488John Reck
581125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    // Returns true iff the surface is now preserving buffers.
591125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    bool setPreserveBuffer(EGLSurface surface, bool preserve);
603b20251a355c88193c439f928a84ae69483fb488John Reck
615515637540bedd8fc9a1a6e46a4b512dd45520a5John Reck    void fence();
625515637540bedd8fc9a1a6e46a4b512dd45520a5John Reck
633b20251a355c88193c439f928a84ae69483fb488John Reckprivate:
643b20251a355c88193c439f928a84ae69483fb488John Reck    friend class RenderThread;
6549796451cb9d1dae580618eb320ef3c5e6d90cd4Chih-Hung Hsieh    explicit EglManager(RenderThread& thread);
663b20251a355c88193c439f928a84ae69483fb488John Reck    // EglContext is never destroyed, method is purposely not implemented
673b20251a355c88193c439f928a84ae69483fb488John Reck    ~EglManager();
683b20251a355c88193c439f928a84ae69483fb488John Reck
69149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    void initExtensions();
70d7db4d767246b41d44995acb93d03d220b53c748John Reck    void createPBufferSurface();
71149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    void loadConfig();
723b20251a355c88193c439f928a84ae69483fb488John Reck    void createContext();
73149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    EGLint queryBufferAge(EGLSurface surface);
7413d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li
753b20251a355c88193c439f928a84ae69483fb488John Reck    RenderThread& mRenderThread;
763b20251a355c88193c439f928a84ae69483fb488John Reck
773b20251a355c88193c439f928a84ae69483fb488John Reck    EGLDisplay mEglDisplay;
783b20251a355c88193c439f928a84ae69483fb488John Reck    EGLConfig mEglConfig;
793b20251a355c88193c439f928a84ae69483fb488John Reck    EGLContext mEglContext;
803b20251a355c88193c439f928a84ae69483fb488John Reck    EGLSurface mPBufferSurface;
813b20251a355c88193c439f928a84ae69483fb488John Reck
823b20251a355c88193c439f928a84ae69483fb488John Reck    EGLSurface mCurrentSurface;
833b20251a355c88193c439f928a84ae69483fb488John Reck
84149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    enum class SwapBehavior {
85149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        Discard,
86149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        Preserved,
87149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        BufferAge,
88149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    };
89149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    SwapBehavior mSwapBehavior = SwapBehavior::Discard;
903b20251a355c88193c439f928a84ae69483fb488John Reck};
913b20251a355c88193c439f928a84ae69483fb488John Reck
923b20251a355c88193c439f928a84ae69483fb488John Reck} /* namespace renderthread */
933b20251a355c88193c439f928a84ae69483fb488John Reck} /* namespace uirenderer */
943b20251a355c88193c439f928a84ae69483fb488John Reck} /* namespace android */
953b20251a355c88193c439f928a84ae69483fb488John Reck
963b20251a355c88193c439f928a84ae69483fb488John Reck#endif /* EGLMANAGER_H */
97