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 #include "WindowSurface.h"
17
18 #include "ErrorLog.h"
19 #include "FbConfig.h"
20 #include "FrameBuffer.h"
21
22 #include "OpenGLESDispatch/EGLDispatch.h"
23
24 #include <assert.h>
25 #include <GLES/glext.h>
26 #include <stdio.h>
27 #include <string.h>
28
29
WindowSurface(EGLDisplay display,EGLConfig config,HandleType hndl)30 WindowSurface::WindowSurface(EGLDisplay display,
31 EGLConfig config,
32 HandleType hndl) :
33 mConfig(config),
34 mDisplay(display),
35 mHndl(hndl) {}
36
~WindowSurface()37 WindowSurface::~WindowSurface() {
38 if (mSurface) {
39 s_egl.eglDestroySurface(mDisplay, mSurface);
40 }
41 }
42
create(EGLDisplay display,EGLConfig config,int p_width,int p_height,HandleType hndl)43 WindowSurface *WindowSurface::create(EGLDisplay display,
44 EGLConfig config,
45 int p_width,
46 int p_height,
47 HandleType hndl) {
48 // allocate space for the WindowSurface object
49 WindowSurface *win = new WindowSurface(display, config, hndl);
50 if (!win) {
51 return NULL;
52 }
53
54 // Create a pbuffer to be used as the egl surface
55 // for that window.
56 if (!win->resize(p_width, p_height)) {
57 delete win;
58 return NULL;
59 }
60
61 return win;
62 }
63
64
setColorBuffer(ColorBufferPtr p_colorBuffer)65 void WindowSurface::setColorBuffer(ColorBufferPtr p_colorBuffer) {
66 mAttachedColorBuffer = p_colorBuffer;
67 if (!p_colorBuffer) return;
68
69 // resize the window if the attached color buffer is of different
70 // size.
71 unsigned int cbWidth = mAttachedColorBuffer->getWidth();
72 unsigned int cbHeight = mAttachedColorBuffer->getHeight();
73
74 if (cbWidth != mWidth || cbHeight != mHeight) {
75 resize(cbWidth, cbHeight);
76 }
77 }
78
bind(RenderContextPtr p_ctx,BindType p_bindType)79 void WindowSurface::bind(RenderContextPtr p_ctx, BindType p_bindType) {
80 if (p_bindType == BIND_READ) {
81 mReadContext = p_ctx;
82 } else if (p_bindType == BIND_DRAW) {
83 mDrawContext = p_ctx;
84 } else if (p_bindType == BIND_READDRAW) {
85 mReadContext = p_ctx;
86 mDrawContext = p_ctx;
87 }
88 }
89
getWidth() const90 GLuint WindowSurface::getWidth() const { return mWidth; }
getHeight() const91 GLuint WindowSurface::getHeight() const { return mHeight; }
92
flushColorBuffer()93 bool WindowSurface::flushColorBuffer() {
94 if (!mAttachedColorBuffer.get()) {
95 return true;
96 }
97 if (!mWidth || !mHeight) {
98 return false;
99 }
100
101 if (mAttachedColorBuffer->getWidth() != mWidth ||
102 mAttachedColorBuffer->getHeight() != mHeight) {
103 // XXX: should never happen - how this needs to be handled?
104 fprintf(stderr, "Dimensions do not match\n");
105 return false;
106 }
107
108 if (!mDrawContext.get()) {
109 fprintf(stderr, "%p: Draw context is NULL\n", this);
110 return false;
111 }
112
113 // Make the surface current
114 EGLContext prevContext = s_egl.eglGetCurrentContext();
115 EGLSurface prevReadSurf = s_egl.eglGetCurrentSurface(EGL_READ);
116 EGLSurface prevDrawSurf = s_egl.eglGetCurrentSurface(EGL_DRAW);
117
118 const bool needToSet = prevContext != mDrawContext->getEGLContext() ||
119 prevReadSurf != mSurface || prevDrawSurf != mSurface;
120 if (needToSet) {
121 if (!s_egl.eglMakeCurrent(mDisplay,
122 mSurface,
123 mSurface,
124 mDrawContext->getEGLContext())) {
125 fprintf(stderr, "Error making draw context current\n");
126 return false;
127 }
128 }
129
130 mAttachedColorBuffer->blitFromCurrentReadBuffer();
131
132 if (needToSet) {
133 // restore current context/surface
134 s_egl.eglMakeCurrent(mDisplay, prevDrawSurf, prevReadSurf, prevContext);
135 }
136
137 return true;
138 }
139
resize(unsigned int p_width,unsigned int p_height)140 bool WindowSurface::resize(unsigned int p_width, unsigned int p_height)
141 {
142 if (mSurface && mWidth == p_width && mHeight == p_height) {
143 // no need to resize
144 return true;
145 }
146
147 EGLContext prevContext = s_egl.eglGetCurrentContext();
148 EGLSurface prevReadSurf = s_egl.eglGetCurrentSurface(EGL_READ);
149 EGLSurface prevDrawSurf = s_egl.eglGetCurrentSurface(EGL_DRAW);
150 EGLSurface prevPbuf = mSurface;
151 bool needRebindContext = mSurface &&
152 (prevReadSurf == mSurface ||
153 prevDrawSurf == mSurface);
154
155 if (needRebindContext) {
156 s_egl.eglMakeCurrent(
157 mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
158 }
159
160 //
161 // Destroy previous surface
162 //
163 if (mSurface) {
164 s_egl.eglDestroySurface(mDisplay, mSurface);
165 mSurface = NULL;
166 }
167
168 //
169 // Create pbuffer surface.
170 //
171 const EGLint pbufAttribs[5] = {
172 EGL_WIDTH, (EGLint) p_width, EGL_HEIGHT, (EGLint) p_height, EGL_NONE,
173 };
174
175 mSurface = s_egl.eglCreatePbufferSurface(mDisplay,
176 mConfig,
177 pbufAttribs);
178 if (mSurface == EGL_NO_SURFACE) {
179 fprintf(stderr, "Renderer error: failed to create/resize pbuffer!!\n");
180 return false;
181 }
182
183 mWidth = p_width;
184 mHeight = p_height;
185
186 if (needRebindContext) {
187 s_egl.eglMakeCurrent(
188 mDisplay,
189 (prevDrawSurf == prevPbuf) ? mSurface : prevDrawSurf,
190 (prevReadSurf == prevPbuf) ? mSurface : prevReadSurf,
191 prevContext);
192 }
193
194 return true;
195 }
196
getHndl() const197 HandleType WindowSurface::getHndl() const {
198 return mHndl;
199 }
200
201 template <class obj_t>
saveHndlOrNull(obj_t obj,android::base::Stream * stream)202 static void saveHndlOrNull(obj_t obj, android::base::Stream* stream) {
203 if (obj) {
204 stream->putBe32(obj->getHndl());
205 } else {
206 stream->putBe32(0);
207 }
208 }
209
onSave(android::base::Stream * stream) const210 void WindowSurface::onSave(android::base::Stream* stream) const {
211 stream->putBe32(getHndl());
212 saveHndlOrNull(mAttachedColorBuffer, stream);
213 saveHndlOrNull(mReadContext, stream);
214 saveHndlOrNull(mDrawContext, stream);
215 stream->putBe32(mWidth);
216 stream->putBe32(mHeight);
217 if (s_egl.eglSaveConfig) {
218 s_egl.eglSaveConfig(mDisplay, mConfig, stream);
219 }
220 }
221
onLoad(android::base::Stream * stream,EGLDisplay display)222 WindowSurface * WindowSurface::onLoad(android::base::Stream* stream,
223 EGLDisplay display) {
224 FrameBuffer* fb = FrameBuffer::getFB();
225 HandleType hndl = stream->getBe32();
226 HandleType cb = stream->getBe32();
227 HandleType readCtx = stream->getBe32();
228 HandleType drawCtx = stream->getBe32();
229
230 GLuint width = stream->getBe32();
231 GLuint height = stream->getBe32();
232 EGLConfig config = 0;
233 if (s_egl.eglLoadConfig) {
234 config = s_egl.eglLoadConfig(display, stream);
235 }
236 WindowSurface* ret = create(display, config, width, height, hndl);
237 assert(ret);
238 // fb is already locked by its caller
239 ret->mAttachedColorBuffer = fb->getColorBuffer_locked(cb);
240 assert(!cb || ret->mAttachedColorBuffer);
241 ret->mReadContext = fb->getContext_locked(readCtx);
242 ret->mDrawContext = fb->getContext_locked(drawCtx);
243 return ret;
244 }
245