• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2018 The Android Open Source Project
2 //
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 #pragma once
16 
17 #include <cinttypes>
18 #include <functional>
19 #include <memory>
20 
21 #include "Hwc2.h"
22 #include "OpenGLESDispatch/GLESv2Dispatch.h"
23 #include "aemu/base/Compiler.h"
24 #include "gl/EmulatedEglContext.h"
25 #include "gl/EmulatedEglFenceSync.h"
26 
27 class OSWindow;
28 
29 namespace gfxstream {
30 
31 class FrameBuffer;
32 struct RenderThreadInfo;
33 
34 // Determines whether the host GPU should be used.
35 bool shouldUseHostGpu();
36 
37 // Determines whether the test will use a Window.
38 bool shouldUseWindow();
39 
40 // Creates or adjusts a persistent test window.
41 // On some systems, test window creation can fail (such as when on a headless server).
42 // In that case, this function will return nullptr.
43 OSWindow* createOrGetTestWindow(int xoffset, int yoffset, int width, int height);
44 
45 
46 class ColorBufferQueue;
47 // Creates a window (or runs headless) to be used in a sample app.
48 class SampleApplication {
49 public:
50  SampleApplication(int windowWidth = 256, int windowHeight = 256, int refreshRate = 60,
51                    gl::GLESApi glVersion = gl::GLESApi_3_0, bool compose = false);
52  virtual ~SampleApplication();
53 
54  // A basic draw loop that works similar to most simple
55  // GL apps that run on desktop.
56  //
57  // Per frame:
58  //
59  // a single GL context for drawing,
60  // a color buffer to blit,
61  // and a call to post that color buffer.
62  void rebind();
63  void drawLoop();
64 
65  // A more complex loop that uses 3 separate contexts
66  // to simulate what goes on in Android:
67  //
68  // Per frame
69  //
70  // a GL 'app' context for drawing,
71  // a SurfaceFlinger context for rendering the "Layer",
72  // and a HWC context for posting.
73  void surfaceFlingerComposerLoop();
74 
75  // TODO:
76  // void HWC2Loop();
77 
78  // Just initialize, draw, and swap buffers once.
79  void drawOnce();
80 
81  bool isSwANGLE();
82 private:
83     void drawWorkerWithCompose(ColorBufferQueue& app2sfQueue, ColorBufferQueue& sf2appQueue);
84     void drawWorker(ColorBufferQueue& app2sfQueue, ColorBufferQueue& sf2appQueue,
85                 ColorBufferQueue& sf2hwcQueue, ColorBufferQueue& hwc2sfQueue);
86     gl::EmulatedEglFenceSync* getFenceSync();
87 
88    protected:
89     virtual void initialize() = 0;
90     virtual void draw() = 0;
91 
92     virtual const gl::GLESv2Dispatch* getGlDispatch();
93 
94     int mWidth = 256;
95     int mHeight = 256;
96     int mRefreshRate = 60;
97 
98     bool mUseSubWindow = false;
99     OSWindow* mWindow = nullptr;
100     FrameBuffer* mFb = nullptr;
101     std::unique_ptr<RenderThreadInfo> mRenderThreadInfo = {};
102 
103     int mXOffset= 400;
104     int mYOffset= 400;
105 
106     unsigned int mColorBuffer = 0;
107     unsigned int mSurface = 0;
108     unsigned int mContext = 0;
109 
110     bool mIsCompose = false;
111     uint32_t mTargetCb = 0;
112 
113     DISALLOW_COPY_ASSIGN_AND_MOVE(SampleApplication);
114 };
115 
116 }  // namespace gfxstream
117