• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 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 _LIBRENDER_WINDOW_SURFACE_H
17 #define _LIBRENDER_WINDOW_SURFACE_H
18 
19 #include "ColorBuffer.h"
20 #include "RenderContext.h"
21 
22 #include <EGL/egl.h>
23 #include <GLES/gl.h>
24 
25 #include <memory>
26 
27 // A class used to model a guest-side window surface. The implementation
28 // uses a host Pbuffer to act as the EGL rendering surface instead.
29 class WindowSurface {
30 public:
31     // Create a new WindowSurface instance.
32     // |display| is the host EGLDisplay value.
33     // |config| is the host EGLConfig value.
34     // |width| and |height| are the initial size of the Pbuffer.
35     // Return a new WindowSurface instance on success, or NULL on failure.
36     static WindowSurface* create(EGLDisplay display,
37                                  EGLConfig config,
38                                  int width,
39                                  int height,
40                                  HandleType hndl);
41 
42     // Destructor.
43     ~WindowSurface();
44 
45     // Retrieve the host EGLSurface of the WindowSurface's Pbuffer.
getEGLSurface()46     EGLSurface getEGLSurface() const { return mSurface; }
47 
48     // Attach a ColorBuffer to this WindowSurface.
49     // Once attached, calling flushColorBuffer() will copy the Pbuffer's
50     // pixels to the color buffer.
51     //
52     // IMPORTANT: This automatically resizes the Pbuffer's to the ColorBuffer's
53     // dimensions. Potentially losing pixel values in the process.
54     void setColorBuffer(ColorBufferPtr p_colorBuffer);
55 
56     // Retrieves a pointer to the attached color buffer.
getAttachedColorBuffer()57     ColorBuffer* getAttachedColorBuffer() const {
58         return mAttachedColorBuffer.get();
59     }
60 
61     // Copy the Pbuffer's pixels to the attached color buffer.
62     // Returns true on success, or false on error (e.g. if there is no
63     // attached color buffer).
64     bool flushColorBuffer();
65 
66     // Used by bind() below.
67     enum BindType {
68         BIND_READ,
69         BIND_DRAW,
70         BIND_READDRAW
71     };
72 
73     // TODO(digit): What is this used for exactly? For example, the
74     // mReadContext is never used by this class. The mDrawContext is only
75     // used temporarily during flushColorBuffer() operation, and could be
76     // passed as a parameter to the function instead. Maybe this is only used
77     // to increment reference counts on the smart pointers.
78     //
79     // Bind a context to the WindowSurface (huh? Normally you would bind a
80     // surface to the context, not the other way around)
81     //
82     // |p_ctx| is a RenderContext pointer.
83     // |p_bindType| is the type of bind. For BIND_READ, this assigns |p_ctx|
84     // to mReadContext, for BIND_DRAW, it assigns it to mDrawContext, and for
85     // for BIND_READDRAW, it assigns it to both.
86     void bind(RenderContextPtr p_ctx, BindType p_bindType);
87 
88     GLuint getWidth() const;
89     GLuint getHeight() const;
90 
91     void onSave(android::base::Stream* stream) const;
92     static WindowSurface *onLoad(android::base::Stream* stream,
93             EGLDisplay display);
94     HandleType getHndl() const;
95 private:
96     WindowSurface(const WindowSurface& other) = delete;
97 
98     WindowSurface(EGLDisplay display, EGLConfig config, HandleType hndl);
99 
100     bool resize(unsigned int p_width, unsigned int p_height);
101 
102 private:
103     EGLSurface mSurface = EGL_NO_SURFACE;
104     ColorBufferPtr mAttachedColorBuffer;
105     RenderContextPtr mReadContext;
106     RenderContextPtr mDrawContext;
107     GLuint mWidth = 0;
108     GLuint mHeight = 0;
109     EGLConfig mConfig = nullptr;
110     EGLDisplay mDisplay = EGL_NO_DISPLAY;
111     HandleType mHndl;
112 };
113 
114 typedef std::shared_ptr<WindowSurface> WindowSurfacePtr;
115 
116 #endif  // _LIBRENDER_WINDOW_SURFACE_H
117