blob: 160fbea318dded6b2eb9148a7356971ff8530a87 [file] [log] [blame]
John Reck23b797a2014-01-03 18:08:34 -08001/*
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
17#define LOG_TAG "CanvasContext"
18
19#include "CanvasContext.h"
20
21#include <cutils/properties.h>
John Reck4f02bf42014-01-03 18:09:17 -080022#include <private/hwui/DrawGlInfo.h>
John Reck23b797a2014-01-03 18:08:34 -080023#include <strings.h>
24
John Reck4f02bf42014-01-03 18:09:17 -080025#include "RenderThread.h"
John Reck23b797a2014-01-03 18:08:34 -080026#include "../Caches.h"
John Reck19b6bcf2014-02-14 20:03:38 -080027#include "../DeferredLayerUpdater.h"
28#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080029#include "../OpenGLRenderer.h"
John Reck23b797a2014-01-03 18:08:34 -080030#include "../Stencil.h"
31
32#define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions"
33#define GLES_VERSION 2
John Reckcdfeef62014-05-14 16:35:46 -070034#define USE_TEXTURE_ATLAS false
John Reck23b797a2014-01-03 18:08:34 -080035
John Reck4f02bf42014-01-03 18:09:17 -080036// Android-specific addition that is used to show when frames began in systrace
37EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
John Reck4f02bf42014-01-03 18:09:17 -080038
John Reck23b797a2014-01-03 18:08:34 -080039namespace android {
40namespace uirenderer {
41namespace renderthread {
42
43#define ERROR_CASE(x) case x: return #x;
44static const char* egl_error_str(EGLint error) {
45 switch (error) {
46 ERROR_CASE(EGL_SUCCESS)
47 ERROR_CASE(EGL_NOT_INITIALIZED)
48 ERROR_CASE(EGL_BAD_ACCESS)
49 ERROR_CASE(EGL_BAD_ALLOC)
50 ERROR_CASE(EGL_BAD_ATTRIBUTE)
51 ERROR_CASE(EGL_BAD_CONFIG)
52 ERROR_CASE(EGL_BAD_CONTEXT)
53 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
54 ERROR_CASE(EGL_BAD_DISPLAY)
55 ERROR_CASE(EGL_BAD_MATCH)
56 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
57 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
58 ERROR_CASE(EGL_BAD_PARAMETER)
59 ERROR_CASE(EGL_BAD_SURFACE)
60 ERROR_CASE(EGL_CONTEXT_LOST)
61 default:
62 return "Unknown error";
63 }
64}
65static const char* egl_error_str() {
66 return egl_error_str(eglGetError());
67}
68
69static bool load_dirty_regions_property() {
70 char buf[PROPERTY_VALUE_MAX];
71 int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true");
72 return !strncasecmp("true", buf, len);
73}
74
75// This class contains the shared global EGL objects, such as EGLDisplay
76// and EGLConfig, which are re-used by CanvasContext
77class GlobalContext {
78public:
79 static GlobalContext* get();
80
John Reck4f02bf42014-01-03 18:09:17 -080081 // Returns true on success, false on failure
82 void initialize();
John Reck23b797a2014-01-03 18:08:34 -080083
John Reck0d1f6342014-03-28 20:30:27 -070084 bool hasContext();
85
John Reck4f02bf42014-01-03 18:09:17 -080086 void usePBufferSurface();
John Reck23b797a2014-01-03 18:08:34 -080087 EGLSurface createSurface(EGLNativeWindowType window);
88 void destroySurface(EGLSurface surface);
89
90 void destroy();
91
92 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
John Reckdbc9a862014-04-17 20:25:13 -070093 // Returns true if the current surface changed, false if it was already current
94 bool makeCurrent(EGLSurface surface);
John Reck4f02bf42014-01-03 18:09:17 -080095 void beginFrame(EGLSurface surface, EGLint* width, EGLint* height);
96 void swapBuffers(EGLSurface surface);
John Reck23b797a2014-01-03 18:08:34 -080097
98 bool enableDirtyRegions(EGLSurface surface);
99
John Reck66f0be62014-05-13 13:39:31 -0700100 void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
101
John Reck23b797a2014-01-03 18:08:34 -0800102private:
103 GlobalContext();
104 // GlobalContext is never destroyed, method is purposely not implemented
105 ~GlobalContext();
106
John Reck4f02bf42014-01-03 18:09:17 -0800107 void loadConfig();
108 void createContext();
109 void initAtlas();
John Reck23b797a2014-01-03 18:08:34 -0800110
111 static GlobalContext* sContext;
112
113 EGLDisplay mEglDisplay;
114 EGLConfig mEglConfig;
115 EGLContext mEglContext;
116 EGLSurface mPBufferSurface;
117
118 const bool mRequestDirtyRegions;
119 bool mCanSetDirtyRegions;
120
121 EGLSurface mCurrentSurface;
John Reck66f0be62014-05-13 13:39:31 -0700122
123 sp<GraphicBuffer> mAtlasBuffer;
124 int64_t* mAtlasMap;
125 size_t mAtlasMapSize;
John Reck23b797a2014-01-03 18:08:34 -0800126};
127
128GlobalContext* GlobalContext::sContext = 0;
129
130GlobalContext* GlobalContext::get() {
131 if (!sContext) {
132 sContext = new GlobalContext();
133 }
134 return sContext;
135}
136
137GlobalContext::GlobalContext()
138 : mEglDisplay(EGL_NO_DISPLAY)
139 , mEglConfig(0)
140 , mEglContext(EGL_NO_CONTEXT)
141 , mPBufferSurface(EGL_NO_SURFACE)
142 , mRequestDirtyRegions(load_dirty_regions_property())
John Reck66f0be62014-05-13 13:39:31 -0700143 , mCurrentSurface(EGL_NO_SURFACE)
144 , mAtlasMap(NULL)
145 , mAtlasMapSize(0) {
John Reck23b797a2014-01-03 18:08:34 -0800146 mCanSetDirtyRegions = mRequestDirtyRegions;
147 ALOGD("Render dirty regions requested: %s", mRequestDirtyRegions ? "true" : "false");
148}
149
John Reck4f02bf42014-01-03 18:09:17 -0800150void GlobalContext::initialize() {
John Reck0d1f6342014-03-28 20:30:27 -0700151 if (hasContext()) return;
John Reck23b797a2014-01-03 18:08:34 -0800152
153 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck4f02bf42014-01-03 18:09:17 -0800154 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
155 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str());
John Reck23b797a2014-01-03 18:08:34 -0800156
157 EGLint major, minor;
John Reck4f02bf42014-01-03 18:09:17 -0800158 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
159 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str());
160
John Reck23b797a2014-01-03 18:08:34 -0800161 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
162
John Reck4f02bf42014-01-03 18:09:17 -0800163 loadConfig();
164 createContext();
165 usePBufferSurface();
166 Caches::getInstance().init();
167 initAtlas();
John Reck23b797a2014-01-03 18:08:34 -0800168}
169
John Reck0d1f6342014-03-28 20:30:27 -0700170bool GlobalContext::hasContext() {
171 return mEglDisplay != EGL_NO_DISPLAY;
172}
173
John Reck4f02bf42014-01-03 18:09:17 -0800174void GlobalContext::loadConfig() {
John Reck23b797a2014-01-03 18:08:34 -0800175 EGLint swapBehavior = mCanSetDirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
176 EGLint attribs[] = {
177 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
178 EGL_RED_SIZE, 8,
179 EGL_GREEN_SIZE, 8,
180 EGL_BLUE_SIZE, 8,
181 EGL_ALPHA_SIZE, 8,
182 EGL_DEPTH_SIZE, 0,
183 EGL_CONFIG_CAVEAT, EGL_NONE,
184 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
185 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
186 EGL_NONE
187 };
188
189 EGLint num_configs = 1;
190 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
191 || num_configs != 1) {
192 // Failed to get a valid config
193 if (mCanSetDirtyRegions) {
194 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
195 // Try again without dirty regions enabled
196 mCanSetDirtyRegions = false;
197 loadConfig();
198 } else {
John Reck4f02bf42014-01-03 18:09:17 -0800199 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
John Reck23b797a2014-01-03 18:08:34 -0800200 }
201 }
John Reck23b797a2014-01-03 18:08:34 -0800202}
203
John Reck4f02bf42014-01-03 18:09:17 -0800204void GlobalContext::createContext() {
John Reck23b797a2014-01-03 18:08:34 -0800205 EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE };
206 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
John Reck4f02bf42014-01-03 18:09:17 -0800207 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
208 "Failed to create context, error = %s", egl_error_str());
John Reck23b797a2014-01-03 18:08:34 -0800209}
210
John Reck66f0be62014-05-13 13:39:31 -0700211void GlobalContext::setTextureAtlas(const sp<GraphicBuffer>& buffer,
212 int64_t* map, size_t mapSize) {
213
214 // Already initialized
215 if (mAtlasBuffer.get()) {
216 ALOGW("Multiple calls to setTextureAtlas!");
217 delete map;
218 return;
219 }
220
221 mAtlasBuffer = buffer;
222 mAtlasMap = map;
223 mAtlasMapSize = mapSize;
224
225 if (hasContext()) {
226 usePBufferSurface();
227 initAtlas();
228 }
229}
230
John Reck4f02bf42014-01-03 18:09:17 -0800231void GlobalContext::initAtlas() {
John Reckcdfeef62014-05-14 16:35:46 -0700232 if (USE_TEXTURE_ATLAS) {
233 Caches::getInstance().assetAtlas.init(mAtlasBuffer, mAtlasMap, mAtlasMapSize);
234 }
John Reck4f02bf42014-01-03 18:09:17 -0800235}
236
237void GlobalContext::usePBufferSurface() {
238 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
239 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck23b797a2014-01-03 18:08:34 -0800240
241 if (mPBufferSurface == EGL_NO_SURFACE) {
242 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
243 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
244 }
John Reck4f02bf42014-01-03 18:09:17 -0800245 makeCurrent(mPBufferSurface);
John Reck23b797a2014-01-03 18:08:34 -0800246}
247
248EGLSurface GlobalContext::createSurface(EGLNativeWindowType window) {
249 initialize();
250 return eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL);
251}
252
253void GlobalContext::destroySurface(EGLSurface surface) {
254 if (isCurrent(surface)) {
255 makeCurrent(EGL_NO_SURFACE);
256 }
257 if (!eglDestroySurface(mEglDisplay, surface)) {
258 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
259 }
260}
261
262void GlobalContext::destroy() {
263 if (mEglDisplay == EGL_NO_DISPLAY) return;
264
265 usePBufferSurface();
266 if (Caches::hasInstance()) {
267 Caches::getInstance().terminate();
268 }
269
270 eglDestroyContext(mEglDisplay, mEglContext);
271 eglDestroySurface(mEglDisplay, mPBufferSurface);
272 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
273 eglTerminate(mEglDisplay);
274 eglReleaseThread();
275
276 mEglDisplay = EGL_NO_DISPLAY;
277 mEglContext = EGL_NO_CONTEXT;
278 mPBufferSurface = EGL_NO_SURFACE;
279 mCurrentSurface = EGL_NO_SURFACE;
280}
281
John Reckdbc9a862014-04-17 20:25:13 -0700282bool GlobalContext::makeCurrent(EGLSurface surface) {
283 if (isCurrent(surface)) return false;
John Reck23b797a2014-01-03 18:08:34 -0800284
285 if (surface == EGL_NO_SURFACE) {
286 // If we are setting EGL_NO_SURFACE we don't care about any of the potential
287 // return errors, which would only happen if mEglDisplay had already been
288 // destroyed in which case the current context is already NO_CONTEXT
289 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
290 } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reck4f02bf42014-01-03 18:09:17 -0800291 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
292 (void*)surface, egl_error_str());
John Reck23b797a2014-01-03 18:08:34 -0800293 }
294 mCurrentSurface = surface;
John Reckdbc9a862014-04-17 20:25:13 -0700295 return true;
John Reck23b797a2014-01-03 18:08:34 -0800296}
297
John Reck4f02bf42014-01-03 18:09:17 -0800298void GlobalContext::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) {
299 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
300 "Tried to beginFrame on EGL_NO_SURFACE!");
301 makeCurrent(surface);
302 if (width) {
303 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width);
John Reck23b797a2014-01-03 18:08:34 -0800304 }
John Reck4f02bf42014-01-03 18:09:17 -0800305 if (height) {
306 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height);
307 }
308 eglBeginFrame(mEglDisplay, surface);
309}
310
311void GlobalContext::swapBuffers(EGLSurface surface) {
312 eglSwapBuffers(mEglDisplay, surface);
313 EGLint err = eglGetError();
John Reck4f02bf42014-01-03 18:09:17 -0800314 LOG_ALWAYS_FATAL_IF(err != EGL_SUCCESS,
315 "Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck23b797a2014-01-03 18:08:34 -0800316}
317
318bool GlobalContext::enableDirtyRegions(EGLSurface surface) {
319 if (!mRequestDirtyRegions) return false;
320
321 if (mCanSetDirtyRegions) {
322 if (!eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED)) {
323 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
324 (void*) surface, egl_error_str());
325 return false;
326 }
327 return true;
328 }
329 // Perhaps it is already enabled?
330 EGLint value;
331 if (!eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &value)) {
332 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
333 (void*) surface, egl_error_str());
334 return false;
335 }
336 return value == EGL_BUFFER_PRESERVED;
337}
338
John Recke45b1fd2014-04-15 09:50:16 -0700339CanvasContext::CanvasContext(bool translucent, RenderNode* rootRenderNode)
John Reck4f02bf42014-01-03 18:09:17 -0800340 : mRenderThread(RenderThread::getInstance())
341 , mEglSurface(EGL_NO_SURFACE)
342 , mDirtyRegionsEnabled(false)
343 , mOpaque(!translucent)
344 , mCanvas(0)
John Recke45b1fd2014-04-15 09:50:16 -0700345 , mHaveNewSurface(false)
346 , mRootRenderNode(rootRenderNode) {
John Reck23b797a2014-01-03 18:08:34 -0800347 mGlobalContext = GlobalContext::get();
348}
349
350CanvasContext::~CanvasContext() {
John Reckfae904d2014-04-14 11:01:57 -0700351 destroyCanvasAndSurface();
John Recke45b1fd2014-04-15 09:50:16 -0700352 mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800353}
354
John Reckfae904d2014-04-14 11:01:57 -0700355void CanvasContext::destroyCanvasAndSurface() {
John Reck4f02bf42014-01-03 18:09:17 -0800356 if (mCanvas) {
357 delete mCanvas;
358 mCanvas = 0;
359 }
John Reck23b797a2014-01-03 18:08:34 -0800360 setSurface(NULL);
361}
362
John Recka5dda642014-05-22 15:43:54 -0700363void CanvasContext::setSurface(ANativeWindow* window) {
364 mNativeWindow = window;
365
John Reck23b797a2014-01-03 18:08:34 -0800366 if (mEglSurface != EGL_NO_SURFACE) {
367 mGlobalContext->destroySurface(mEglSurface);
368 mEglSurface = EGL_NO_SURFACE;
369 }
370
371 if (window) {
372 mEglSurface = mGlobalContext->createSurface(window);
John Reck4f02bf42014-01-03 18:09:17 -0800373 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
374 "Failed to create EGLSurface for window %p, eglErr = %s",
375 (void*) window, egl_error_str());
John Reck23b797a2014-01-03 18:08:34 -0800376 }
377
378 if (mEglSurface != EGL_NO_SURFACE) {
379 mDirtyRegionsEnabled = mGlobalContext->enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -0800380 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -0700381 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -0700382 } else {
383 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -0800384 }
John Reck23b797a2014-01-03 18:08:34 -0800385}
386
John Reck4f02bf42014-01-03 18:09:17 -0800387void CanvasContext::swapBuffers() {
388 mGlobalContext->swapBuffers(mEglSurface);
389 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800390}
391
John Reckf7d9c1d2014-04-09 10:01:03 -0700392void CanvasContext::requireSurface() {
393 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
394 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700395 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800396}
397
John Recka5dda642014-05-22 15:43:54 -0700398bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800399 if (mCanvas) return false;
400 setSurface(window);
John Reck4f02bf42014-01-03 18:09:17 -0800401 mCanvas = new OpenGLRenderer();
402 mCanvas->initProperties();
403 return true;
404}
405
John Recka5dda642014-05-22 15:43:54 -0700406void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800407 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700408}
409
John Recka5dda642014-05-22 15:43:54 -0700410void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700411 // TODO: For now we just need a fence, in the future suspend any animations
412 // and such to prevent from trying to render into this surface
John Reck4f02bf42014-01-03 18:09:17 -0800413}
414
Chris Craik797b95b2014-05-20 18:10:25 -0700415void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius) {
John Reck4f02bf42014-01-03 18:09:17 -0800416 if (!mCanvas) return;
417 mCanvas->setViewport(width, height);
Chris Craik797b95b2014-05-20 18:10:25 -0700418 mCanvas->initializeLight(lightCenter, lightRadius);
John Reck4f02bf42014-01-03 18:09:17 -0800419}
420
John Reck63a06672014-05-07 13:45:54 -0700421void CanvasContext::setOpaque(bool opaque) {
422 mOpaque = opaque;
423}
424
John Reck860d1552014-04-11 19:15:05 -0700425void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700426 // TODO: Figure out why this workaround is needed, see b/13913604
427 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
428 mHaveNewSurface |= mGlobalContext->makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700429}
430
John Reckf9be7792014-05-02 18:21:16 -0700431void CanvasContext::prepareDraw(const Vector<DeferredLayerUpdater*>* layerUpdaters,
432 TreeInfo& info) {
433 LOG_ALWAYS_FATAL_IF(!mCanvas, "Cannot prepareDraw without a canvas!");
434 makeCurrent();
435
436 processLayerUpdates(layerUpdaters, info);
437 if (info.out.hasAnimations) {
438 // TODO: Uh... crap?
439 }
440 prepareTree(info);
441}
442
John Reck860d1552014-04-11 19:15:05 -0700443void CanvasContext::processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters,
444 TreeInfo& info) {
John Reck19b6bcf2014-02-14 20:03:38 -0800445 for (size_t i = 0; i < layerUpdaters->size(); i++) {
446 DeferredLayerUpdater* update = layerUpdaters->itemAt(i);
John Reck860d1552014-04-11 19:15:05 -0700447 bool success = update->apply(info);
448 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
John Reck19b6bcf2014-02-14 20:03:38 -0800449 if (update->backingLayer()->deferredUpdateScheduled) {
450 mCanvas->pushLayerUpdate(update->backingLayer());
451 }
452 }
453}
454
John Recke45b1fd2014-04-15 09:50:16 -0700455void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700456 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700457
John Reckf9be7792014-05-02 18:21:16 -0700458 info.frameTimeMs = mRenderThread.timeLord().frameTimeMs();
John Recke45b1fd2014-04-15 09:50:16 -0700459 mRootRenderNode->prepareTree(info);
460
John Recka5dda642014-05-22 15:43:54 -0700461 int runningBehind = 0;
462 // TODO: This query is moderately expensive, investigate adding some sort
463 // of fast-path based off when we last called eglSwapBuffers() as well as
464 // last vsync time. Or something.
465 mNativeWindow->query(mNativeWindow.get(),
466 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
467 info.out.canDrawThisFrame = !runningBehind;
468
469 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckf9be7792014-05-02 18:21:16 -0700470 if (info.out.hasFunctors) {
471 info.out.requiresUiRedraw = true;
472 } else if (!info.out.requiresUiRedraw) {
473 // If animationsNeedsRedraw is set don't bother posting for an RT anim
474 // as we will just end up fighting the UI thread.
475 mRenderThread.postFrameCallback(this);
476 }
John Recke45b1fd2014-04-15 09:50:16 -0700477 }
478}
479
John Recka5dda642014-05-22 15:43:54 -0700480void CanvasContext::notifyFramePending() {
481 ATRACE_CALL();
482 mRenderThread.pushBackFrameCallback(this);
483}
484
John Recke45b1fd2014-04-15 09:50:16 -0700485void CanvasContext::draw(Rect* dirty) {
John Reck4f02bf42014-01-03 18:09:17 -0800486 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
487 "drawDisplayList called on a context with no canvas or surface!");
488
489 EGLint width, height;
490 mGlobalContext->beginFrame(mEglSurface, &width, &height);
491 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
492 mCanvas->setViewport(width, height);
493 dirty = NULL;
494 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
495 dirty = NULL;
496 }
497
498 status_t status;
John Recke45b1fd2014-04-15 09:50:16 -0700499 if (dirty && !dirty->isEmpty()) {
John Reck4f02bf42014-01-03 18:09:17 -0800500 status = mCanvas->prepareDirty(dirty->left, dirty->top,
501 dirty->right, dirty->bottom, mOpaque);
502 } else {
503 status = mCanvas->prepare(mOpaque);
504 }
505
506 Rect outBounds;
John Recke45b1fd2014-04-15 09:50:16 -0700507 status |= mCanvas->drawDisplayList(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800508
509 // TODO: Draw debug info
510 // TODO: Performance tracking
511
512 mCanvas->finish();
513
514 if (status & DrawGlInfo::kStatusDrew) {
515 swapBuffers();
516 }
517}
518
John Recke45b1fd2014-04-15 09:50:16 -0700519// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700520void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700521 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
522 return;
523 }
524
John Recke45b1fd2014-04-15 09:50:16 -0700525 ATRACE_CALL();
526
527 TreeInfo info;
528 info.evaluateAnimations = true;
John Recke45b1fd2014-04-15 09:50:16 -0700529 info.performStagingPush = false;
530 info.prepareTextures = false;
531
532 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700533 if (info.out.canDrawThisFrame) {
534 draw(NULL);
535 }
John Recke45b1fd2014-04-15 09:50:16 -0700536}
537
John Reck0d1f6342014-03-28 20:30:27 -0700538void CanvasContext::invokeFunctor(Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700539 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700540 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
541 if (mGlobalContext->hasContext()) {
542 requireGlContext();
543 mode = DrawGlInfo::kModeProcess;
544 }
John Reck832b15142014-05-07 14:39:44 -0700545 (*functor)(mode, NULL);
John Reck6f07a0d2014-04-16 21:31:25 -0700546
547 if (mCanvas) {
548 mCanvas->resume();
549 }
John Reck23b797a2014-01-03 18:08:34 -0800550}
551
John Reck19b6bcf2014-02-14 20:03:38 -0800552bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
553 requireGlContext();
John Reck860d1552014-04-11 19:15:05 -0700554 TreeInfo info;
555 layer->apply(info);
John Reck19b6bcf2014-02-14 20:03:38 -0800556 return LayerRenderer::copyLayer(layer->backingLayer(), bitmap);
557}
558
John Recke1628b72014-05-23 15:11:19 -0700559void CanvasContext::flushCaches(Caches::FlushMode flushMode) {
560 if (mGlobalContext->hasContext()) {
561 requireGlContext();
562 Caches::getInstance().flush(flushMode);
563 }
564}
565
John Reckfc53ef272014-02-11 10:40:25 -0800566void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800567 requireGlContext();
568 task->run();
569}
570
John Reck1949e792014-04-08 15:18:56 -0700571Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700572 requireSurface();
John Reck1949e792014-04-08 15:18:56 -0700573 return LayerRenderer::createRenderLayer(width, height);
574}
575
576Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700577 requireSurface();
John Reck1949e792014-04-08 15:18:56 -0700578 return LayerRenderer::createTextureLayer();
579}
580
John Reck19b6bcf2014-02-14 20:03:38 -0800581void CanvasContext::requireGlContext() {
John Reckfc53ef272014-02-11 10:40:25 -0800582 if (mEglSurface != EGL_NO_SURFACE) {
John Reckdbc9a862014-04-17 20:25:13 -0700583 makeCurrent();
John Reckfc53ef272014-02-11 10:40:25 -0800584 } else {
585 mGlobalContext->usePBufferSurface();
586 }
John Reckfc53ef272014-02-11 10:40:25 -0800587}
588
John Reck66f0be62014-05-13 13:39:31 -0700589void CanvasContext::setTextureAtlas(const sp<GraphicBuffer>& buffer,
590 int64_t* map, size_t mapSize) {
591 GlobalContext::get()->setTextureAtlas(buffer, map, mapSize);
592}
593
John Reck23b797a2014-01-03 18:08:34 -0800594} /* namespace renderthread */
595} /* namespace uirenderer */
596} /* namespace android */