1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
2a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk * Copyright (C) 2011 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17e23d239828a229eb7d4d33c9630070f0a87833e1Alex Sakhartchouk#include "rs.h"
18326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsDevice.h"
19326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsContext.h"
20326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsThreadIO.h"
2193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
22397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray#include "rsgApiStructs.h"
23397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray
2493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
254edf030cbb7c6ac08dc563335c2af73c20f6e2e5Alex Sakhartchouk#include "rsMesh.h"
265f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams#include <gui/DisplayEventReceiver.h>
2793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
28326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
29158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams#include <sys/types.h>
30158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams#include <sys/resource.h>
317bf29ddc35450d8064541c42c99a1f48be6cf0ddJason Sams#include <sched.h>
32158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams
338d957fa762eff6c03a93ddea9405d9575665f1ecJason Sams#include <sys/syscall.h>
3493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#include <string.h>
35414a46166126da6864258bd25ff183f9a3c6261dStephen Hines#include <dlfcn.h>
36f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers#include <inttypes.h>
37b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#include <unistd.h>
38414a46166126da6864258bd25ff183f9a3c6261dStephen Hines
3916647800aaaf6c8a7f8259e97f2200bec2cc0a43Stephen Hines#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && \
4016647800aaaf6c8a7f8259e97f2200bec2cc0a43Stephen Hines        defined(HAVE_ANDROID_OS)
410b575de8ed0b628d84d256f5846500b0385979bdTim Murray#include <cutils/properties.h>
420b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
430b575de8ed0b628d84d256f5846500b0385979bdTim Murray
446dfe6841d357a1664433f0dcec3c8f4ae8ffbeb0Stephen Hines#ifdef RS_COMPATIBILITY_LIB
456dfe6841d357a1664433f0dcec3c8f4ae8ffbeb0Stephen Hines#include "rsCompatibilityLib.h"
466dfe6841d357a1664433f0dcec3c8f4ae8ffbeb0Stephen Hines#endif
476dfe6841d357a1664433f0dcec3c8f4ae8ffbeb0Stephen Hines
480b575de8ed0b628d84d256f5846500b0385979bdTim Murray#ifdef RS_SERVER
490b575de8ed0b628d84d256f5846500b0385979bdTim Murray// Android exposes gettid(), standard Linux does not
500b575de8ed0b628d84d256f5846500b0385979bdTim Murraystatic pid_t gettid() {
510b575de8ed0b628d84d256f5846500b0385979bdTim Murray    return syscall(SYS_gettid);
520b575de8ed0b628d84d256f5846500b0385979bdTim Murray}
530b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
540b575de8ed0b628d84d256f5846500b0385979bdTim Murray
55326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android;
56326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android::renderscript;
57326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
58fb03a22ad2adadc1ff50a8b50d43ad7fcc3fa6edJason Samspthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
594961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Samspthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
60ca3f09c0924e9515901dfd47fa5f95385d53cf80Stephen Hinespthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
61326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
62afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukbool Context::initGLThread() {
636b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    pthread_mutex_lock(&gInitMutex);
646b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams
654b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!mHal.funcs.initGraphics(this)) {
665c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams        pthread_mutex_unlock(&gInitMutex);
67af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p initGraphics failed", this);
685c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams        return false;
696b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    }
706b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams
716b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    pthread_mutex_unlock(&gInitMutex);
725c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    return true;
73326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
74326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
75afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::deinitEGL() {
7693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
774b3de47071d875faaa7d419d050a464b09538797Jason Sams    mHal.funcs.shutdownGraphics(this);
7893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
7933b6e3b91329080e5cdd0b8fdbcd3e6a906032aeJason Sams}
8033b6e3b91329080e5cdd0b8fdbcd3e6a906032aeJason Sams
8160709257bbdeb0c50f39b9c8969dc76264d6e142Jason SamsContext::PushState::PushState(Context *con) {
8260709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    mRsc = con;
8393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
84c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams    if (con->mIsGraphicsContext) {
85c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mFragment.set(con->getProgramFragment());
86c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mVertex.set(con->getProgramVertex());
87c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mStore.set(con->getProgramStore());
88c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mRaster.set(con->getProgramRaster());
89c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mFont.set(con->getFont());
90c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams    }
9193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
9260709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams}
9360709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams
9460709257bbdeb0c50f39b9c8969dc76264d6e142Jason SamsContext::PushState::~PushState() {
9593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
96c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams    if (mRsc->mIsGraphicsContext) {
97c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mRsc->setProgramFragment(mFragment.get());
98c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mRsc->setProgramVertex(mVertex.get());
99c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mRsc->setProgramStore(mStore.get());
100c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mRsc->setProgramRaster(mRaster.get());
101c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams        mRsc->setFont(mFont.get());
102c946b614ee6c983215cc3de7834a7a334f860d68Jason Sams    }
10393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
10460709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams}
10560709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams
10633b6e3b91329080e5cdd0b8fdbcd3e6a906032aeJason Sams
107afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukuint32_t Context::runScript(Script *s) {
108da3b58d804b59595f1c618ff271fecd8a9ce59edShih-wei Liao    PushState ps(this);
1091030893d9b99b72468034da13df025bda479bb97Jason Sams
110c61346b91434307c5003029017b54ce9c49112beJason Sams    uint32_t ret = s->run(this);
111c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    return ret;
1121030893d9b99b72468034da13df025bda479bb97Jason Sams}
1131030893d9b99b72468034da13df025bda479bb97Jason Sams
114afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukuint32_t Context::runRootScript() {
1152dca84dd6c07992f78ad050177975f16486dd77eJason Sams    timerSet(RS_TIMER_SCRIPT);
11693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
1178c401effb0837155fc39ca0364f57a882d127d38Jason Sams    mStateFragmentStore.mLast.clear();
11893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
1192382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    watchdog.inRoot = true;
120c61346b91434307c5003029017b54ce9c49112beJason Sams    uint32_t ret = runScript(mRootScript.get());
1212382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    watchdog.inRoot = false;
1228cfdd24fec22080b72266d33f61befc4a98b77c6Jason Sams
12324371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    return ret;
12424371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams}
125cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams
126afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukuint64_t Context::getTime() const {
127b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk#ifndef ANDROID_RS_SERIALIZE
12824371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    struct timespec t;
12924371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    clock_gettime(CLOCK_MONOTONIC, &t);
13024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
131b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk#else
132b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    return 0;
133b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk#endif //ANDROID_RS_SERIALIZE
13424371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams}
135cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams
136afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::timerReset() {
13724371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
13824371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        mTimers[ct] = 0;
13924371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    }
14024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams}
141cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams
142afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::timerInit() {
14324371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    mTimeLast = getTime();
1441d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    mTimeFrame = mTimeLast;
1451d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    mTimeLastFrame = mTimeLast;
14624371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    mTimerActive = RS_TIMER_INTERNAL;
147c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    mAverageFPSFrameCount = 0;
148c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    mAverageFPSStartTime = mTimeLast;
149c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    mAverageFPS = 0;
15024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    timerReset();
15124371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams}
15224371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams
153afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::timerFrame() {
1541d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    mTimeLastFrame = mTimeFrame;
1551d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    mTimeFrame = getTime();
156c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    // Update average fps
157c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    const uint64_t averageFramerateInterval = 1000 * 1000000;
158c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    mAverageFPSFrameCount ++;
159c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
160afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (inverval >= averageFramerateInterval) {
161c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk        inverval = inverval / 1000000;
162c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk        mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
163c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk        mAverageFPSFrameCount = 0;
164c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk        mAverageFPSStartTime = mTimeFrame;
165c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    }
1661d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams}
1671d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams
168afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::timerSet(Timers tm) {
16924371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    uint64_t last = mTimeLast;
17024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    mTimeLast = getTime();
17124371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    mTimers[mTimerActive] += mTimeLast - last;
17224371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    mTimerActive = tm;
17324371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams}
17424371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams
175afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::timerPrint() {
17624371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    double total = 0;
17724371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
17824371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        total += mTimers[ct];
17924371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    }
1801d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    uint64_t frame = mTimeFrame - mTimeLastFrame;
1812dca84dd6c07992f78ad050177975f16486dd77eJason Sams    mTimeMSLastFrame = frame / 1000000;
1822dca84dd6c07992f78ad050177975f16486dd77eJason Sams    mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
1832dca84dd6c07992f78ad050177975f16486dd77eJason Sams    mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
18424371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams
1852dca84dd6c07992f78ad050177975f16486dd77eJason Sams
1862dca84dd6c07992f78ad050177975f16486dd77eJason Sams    if (props.mLogTimes) {
187f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers        ALOGV("RS: Frame (%i),   Script %2.1f%% (%i),  Swap %2.1f%% (%i),  Idle %2.1f%% (%" PRIi64 "),  "
188f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers              "Internal %2.1f%% (%" PRIi64 "), Avg fps: %u",
1892dca84dd6c07992f78ad050177975f16486dd77eJason Sams             mTimeMSLastFrame,
1902dca84dd6c07992f78ad050177975f16486dd77eJason Sams             100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
1912dca84dd6c07992f78ad050177975f16486dd77eJason Sams             100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
1922dca84dd6c07992f78ad050177975f16486dd77eJason Sams             100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
193c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk             100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
194c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk             mAverageFPS);
1952dca84dd6c07992f78ad050177975f16486dd77eJason Sams    }
196326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
197326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
198afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukbool Context::setupCheck() {
19993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
200721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mFragmentStore->setup(this, &mStateFragmentStore);
201c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    mFragment->setup(this, &mStateFragment);
202721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mRaster->setup(this, &mStateRaster);
203c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    mVertex->setup(this, &mStateVertex);
204c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    mFBOCache.setup(this);
20593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
206a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams    return true;
207326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
208326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
20993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
210889fe50e7aaebed8cb8284b16a0e51e64e8a3a9cAlex Sakhartchoukvoid Context::setupProgramStore() {
211721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mFragmentStore->setup(this, &mStateFragmentStore);
212889fe50e7aaebed8cb8284b16a0e51e64e8a3a9cAlex Sakhartchouk}
21393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
214889fe50e7aaebed8cb8284b16a0e51e64e8a3a9cAlex Sakhartchouk
215d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Samsstatic uint32_t getProp(const char *str) {
2167e85ca20affa9655d9033ec2b0fdff034f19a9bfNick Kralevich#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
21776371fff76412fd020e24ddb8bf1ddb5c75f0ed1Joe Onorato    char buf[PROPERTY_VALUE_MAX];
2181fddd90849deaae89b546ff492c345d485bbce42Jason Sams    property_get(str, buf, "0");
219d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    return atoi(buf);
2200b575de8ed0b628d84d256f5846500b0385979bdTim Murray#else
2210b575de8ed0b628d84d256f5846500b0385979bdTim Murray    return 0;
2220b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
22376371fff76412fd020e24ddb8bf1ddb5c75f0ed1Joe Onorato}
224326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
225afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::displayDebugStats() {
22693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
2270cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk    char buffer[128];
228c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
229ca5a454e022caec6c6d3cbb404cc09ea095ba97aAlex Sakhartchouk    float oldR, oldG, oldB, oldA;
230ca5a454e022caec6c6d3cbb404cc09ea095ba97aAlex Sakhartchouk    mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
23109c67356bbeee0a97a20a06c95b66756838cb541Alex Sakhartchouk    uint32_t bufferLen = strlen(buffer);
2320cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk
2331809bde133e0d66f06cea65887d9ceb3c70f8b95Alex Sakhartchouk    ObjectBaseRef<Font> lastFont(getFont());
2341809bde133e0d66f06cea65887d9ceb3c70f8b95Alex Sakhartchouk    setFont(NULL);
23509c67356bbeee0a97a20a06c95b66756838cb541Alex Sakhartchouk    float shadowCol = 0.1f;
236ca5a454e022caec6c6d3cbb404cc09ea095ba97aAlex Sakhartchouk    mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
237c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
2380cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk
23909c67356bbeee0a97a20a06c95b66756838cb541Alex Sakhartchouk    mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
240c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
2410cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk
2421809bde133e0d66f06cea65887d9ceb3c70f8b95Alex Sakhartchouk    setFont(lastFont.get());
243ca5a454e022caec6c6d3cbb404cc09ea095ba97aAlex Sakhartchouk    mStateFont.setFontColor(oldR, oldG, oldB, oldA);
24493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
2450cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk}
2460cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk
2470c66f0795ad0b45934502820df6d718b9096edfaTim Murraybool Context::loadRuntime(const char* filename, Context* rsc) {
2480c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2490c66f0795ad0b45934502820df6d718b9096edfaTim Murray    // TODO: store the driverSO somewhere so we can dlclose later
2500c66f0795ad0b45934502820df6d718b9096edfaTim Murray    void *driverSO = NULL;
2510c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2520c66f0795ad0b45934502820df6d718b9096edfaTim Murray    driverSO = dlopen(filename, RTLD_LAZY);
2530c66f0795ad0b45934502820df6d718b9096edfaTim Murray    if (driverSO == NULL) {
2540c66f0795ad0b45934502820df6d718b9096edfaTim Murray        ALOGE("Failed loading RS driver: %s", dlerror());
2550c66f0795ad0b45934502820df6d718b9096edfaTim Murray        return false;
2560c66f0795ad0b45934502820df6d718b9096edfaTim Murray    }
2570c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2580c66f0795ad0b45934502820df6d718b9096edfaTim Murray    // Need to call dlerror() to clear buffer before using it for dlsym().
2590c66f0795ad0b45934502820df6d718b9096edfaTim Murray    (void) dlerror();
2600c66f0795ad0b45934502820df6d718b9096edfaTim Murray    typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
2610c66f0795ad0b45934502820df6d718b9096edfaTim Murray    HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
2620c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2630c66f0795ad0b45934502820df6d718b9096edfaTim Murray    // If we can't find the C variant, we go looking for the C++ version.
2640c66f0795ad0b45934502820df6d718b9096edfaTim Murray    if (halInit == NULL) {
2650c66f0795ad0b45934502820df6d718b9096edfaTim Murray        ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
2660c66f0795ad0b45934502820df6d718b9096edfaTim Murray        halInit = (HalSig) dlsym(driverSO,
2670c66f0795ad0b45934502820df6d718b9096edfaTim Murray                "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
2680c66f0795ad0b45934502820df6d718b9096edfaTim Murray    }
2690c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2700c66f0795ad0b45934502820df6d718b9096edfaTim Murray    if (halInit == NULL) {
2710c66f0795ad0b45934502820df6d718b9096edfaTim Murray        dlclose(driverSO);
2720c66f0795ad0b45934502820df6d718b9096edfaTim Murray        ALOGE("Failed to find rsdHalInit: %s", dlerror());
2730c66f0795ad0b45934502820df6d718b9096edfaTim Murray        return false;
2740c66f0795ad0b45934502820df6d718b9096edfaTim Murray    }
2750c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2760c66f0795ad0b45934502820df6d718b9096edfaTim Murray    if (!(*halInit)(rsc, 0, 0)) {
2770c66f0795ad0b45934502820df6d718b9096edfaTim Murray        dlclose(driverSO);
2780c66f0795ad0b45934502820df6d718b9096edfaTim Murray        ALOGE("Hal init failed");
2790c66f0795ad0b45934502820df6d718b9096edfaTim Murray        return false;
2800c66f0795ad0b45934502820df6d718b9096edfaTim Murray    }
2810c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2820c66f0795ad0b45934502820df6d718b9096edfaTim Murray    //validate HAL struct
2830c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2840c66f0795ad0b45934502820df6d718b9096edfaTim Murray
2850c66f0795ad0b45934502820df6d718b9096edfaTim Murray    return true;
2860c66f0795ad0b45934502820df6d718b9096edfaTim Murray}
2870c66f0795ad0b45934502820df6d718b9096edfaTim Murray
288110f181b7966212a36ef18016f9b81c7322d0a2fJason Samsextern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
289110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams
290afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid * Context::threadProc(void *vrsc) {
29183c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    Context *rsc = static_cast<Context *>(vrsc);
292b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk#ifndef ANDROID_RS_SERIALIZE
29383c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    rsc->mNativeThreadId = gettid();
29493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
2954d252d6e807b89764dad123ac845df298c52ca97Tim Murray    if (!rsc->isSynchronous()) {
2964d252d6e807b89764dad123ac845df298c52ca97Tim Murray        setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
2974d252d6e807b89764dad123ac845df298c52ca97Tim Murray    }
29883c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
29993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#else
30093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    if (!rsc->isSynchronous()) {
30193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams        setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
30293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    }
30393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    rsc->mThreadPriority = -4;
30493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
305b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk#endif //ANDROID_RS_SERIALIZE
306d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
307d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
308d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
309d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
310d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
311d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
312d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
313d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams    rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
31483c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
3152d8030adb222044345099cf1e3c7998c71c82297Jason Sams    if (getProp("debug.rs.debug") != 0) {
3162d8030adb222044345099cf1e3c7998c71c82297Jason Sams        ALOGD("Forcing debug context due to debug.rs.debug.");
3172d8030adb222044345099cf1e3c7998c71c82297Jason Sams        rsc->mContextType = RS_CONTEXT_TYPE_DEBUG;
3182d8030adb222044345099cf1e3c7998c71c82297Jason Sams    }
3192d8030adb222044345099cf1e3c7998c71c82297Jason Sams
3200c66f0795ad0b45934502820df6d718b9096edfaTim Murray    bool loadDefault = true;
32191dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines
32291dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines    // Provide a mechanism for dropping in a different RS driver.
323110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams#ifndef RS_COMPATIBILITY_LIB
32491dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#ifdef OVERRIDE_RS_DRIVER
32591dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#define XSTR(S) #S
32691dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#define STR(S) XSTR(S)
32791dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
3280c66f0795ad0b45934502820df6d718b9096edfaTim Murray
329c83dd0b9f81aaeb15e2d03849b04f20b581bfe49Stephen Hines    if (getProp("debug.rs.default-CPU-driver") != 0) {
33043463928546cbd5d3a73b67f4931922b94023c5eStephen Hines        ALOGD("Skipping override driver and loading default CPU driver");
331cd5f1cef34f23262f59e820a5f65ec4dad7f0bf8Jason Sams    } else if (rsc->mForceCpu || rsc->mIsGraphicsContext) {
3320e92fa3775b3537c29a7a0ca603d892548936038Tim Murray        ALOGV("Application requested CPU execution");
333cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
334cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines        ALOGV("Application requested debug context");
335c83dd0b9f81aaeb15e2d03849b04f20b581bfe49Stephen Hines    } else {
3366e6cdbc303bef581cfb7d1fd0902cb42c89010ccTim Murray#if defined(__LP64__) && defined(DISABLE_RS_64_BIT_DRIVER)
3376e6cdbc303bef581cfb7d1fd0902cb42c89010ccTim Murray        // skip load
3386e6cdbc303bef581cfb7d1fd0902cb42c89010ccTim Murray#else
3390c66f0795ad0b45934502820df6d718b9096edfaTim Murray        if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
34043463928546cbd5d3a73b67f4931922b94023c5eStephen Hines            ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
3410c66f0795ad0b45934502820df6d718b9096edfaTim Murray            loadDefault = false;
3420c66f0795ad0b45934502820df6d718b9096edfaTim Murray        } else {
3430c66f0795ad0b45934502820df6d718b9096edfaTim Murray            ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
344c83dd0b9f81aaeb15e2d03849b04f20b581bfe49Stephen Hines        }
3456e6cdbc303bef581cfb7d1fd0902cb42c89010ccTim Murray#endif
34691dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines    }
347c83dd0b9f81aaeb15e2d03849b04f20b581bfe49Stephen Hines
34891dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#undef XSTR
34991dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#undef STR
35091dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines#endif  // OVERRIDE_RS_DRIVER
35191dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines
3520c66f0795ad0b45934502820df6d718b9096edfaTim Murray    if (loadDefault) {
3530c66f0795ad0b45934502820df6d718b9096edfaTim Murray        if (!loadRuntime("libRSDriver.so", rsc)) {
3540c66f0795ad0b45934502820df6d718b9096edfaTim Murray            ALOGE("Failed to load default runtime!");
3556f01bcf691433b8413e57fe8b67b0f5dba4d0013Stephen Hines            rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
35691dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines            return NULL;
35791dfcdb0f9d5402c26dd65d26d1163f05c2f1e50Stephen Hines        }
358414a46166126da6864258bd25ff183f9a3c6261dStephen Hines    }
359110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams#else // RS_COMPATIBILITY_LIB
360110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams    if (rsdHalInit(rsc, 0, 0) != true) {
361110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams        return NULL;
362110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams    }
363110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams#endif
364110f181b7966212a36ef18016f9b81c7322d0a2fJason Sams
365414a46166126da6864258bd25ff183f9a3c6261dStephen Hines
36683c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
36783c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
36893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
36983c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    if (rsc->mIsGraphicsContext) {
370d3e7107c60ce06d637d83fa0db783ecdd0ad534cJason Sams        if (!rsc->initGLThread()) {
371d3e7107c60ce06d637d83fa0db783ecdd0ad534cJason Sams            rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
372d3e7107c60ce06d637d83fa0db783ecdd0ad534cJason Sams            return NULL;
373d3e7107c60ce06d637d83fa0db783ecdd0ad534cJason Sams        }
374d3e7107c60ce06d637d83fa0db783ecdd0ad534cJason Sams
37583c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->mStateRaster.init(rsc);
37683c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->setProgramRaster(NULL);
37783c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->mStateVertex.init(rsc);
37883c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->setProgramVertex(NULL);
37983c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->mStateFragment.init(rsc);
38083c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->setProgramFragment(NULL);
38183c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->mStateFragmentStore.init(rsc);
38283c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->setProgramStore(NULL);
38383c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->mStateFont.init(rsc);
38483c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->setFont(NULL);
385c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        rsc->mStateSampler.init(rsc);
386da6d34a5a6ece8c30d20673b9b6ff07d8c91768bAlex Sakhartchouk        rsc->mFBOCache.init(rsc);
38783c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    }
38893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
38983c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
39083c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    rsc->mRunning = true;
3914d252d6e807b89764dad123ac845df298c52ca97Tim Murray
3924d252d6e807b89764dad123ac845df298c52ca97Tim Murray    if (rsc->isSynchronous()) {
3934d252d6e807b89764dad123ac845df298c52ca97Tim Murray        return NULL;
3944d252d6e807b89764dad123ac845df298c52ca97Tim Murray    }
3954d252d6e807b89764dad123ac845df298c52ca97Tim Murray
3965f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams    if (!rsc->mIsGraphicsContext) {
3975f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        while (!rsc->mExit) {
398963a2fb9f03c88633bc67c4a1789429b9a482091Jason Sams            rsc->mIO.playCoreCommands(rsc, -1);
399e0aab4a8ff1cffd8cfaedc2623db94072549e0e5Jason Sams        }
40093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
4015f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams    } else {
4025f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams#ifndef ANDROID_RS_SERIALIZE
4035f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        DisplayEventReceiver displayEvent;
4045f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        DisplayEventReceiver::Event eventBuffer[1];
4055f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams#endif
4065f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        int vsyncRate = 0;
4075f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        int targetRate = 0;
408e0aab4a8ff1cffd8cfaedc2623db94072549e0e5Jason Sams
4095f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        bool drawOnce = false;
4105f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        while (!rsc->mExit) {
4115f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            rsc->timerSet(RS_TIMER_IDLE);
41283c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
4135f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams#ifndef ANDROID_RS_SERIALIZE
4149afd9a5c712f088e62ee2595e40e0f4584887443Jason Sams            if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
4159afd9a5c712f088e62ee2595e40e0f4584887443Jason Sams                targetRate = 0;
4169afd9a5c712f088e62ee2595e40e0f4584887443Jason Sams            }
4179afd9a5c712f088e62ee2595e40e0f4584887443Jason Sams
4185f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            if (vsyncRate != targetRate) {
4195f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                displayEvent.setVsyncRate(targetRate);
4205f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                vsyncRate = targetRate;
4215f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            }
4225f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            if (targetRate) {
423963a2fb9f03c88633bc67c4a1789429b9a482091Jason Sams                drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
4245f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                while (displayEvent.getEvents(eventBuffer, 1) != 0) {
4255f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                    //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
4265f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                }
4275f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            } else
4285f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams#endif
4295f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            {
430963a2fb9f03c88633bc67c4a1789429b9a482091Jason Sams                drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
43183c451a4ef4388e002482e383d488ca9b7b7600dJason Sams            }
43283c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
4335f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
4345f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                (targetRate || drawOnce) && !rsc->mPaused) {
4355f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams
4365f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                drawOnce = false;
4375f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                targetRate = ((rsc->runRootScript() + 15) / 16);
4385f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams
4395f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                if (rsc->props.mLogVisual) {
4405f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                    rsc->displayDebugStats();
4415f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                }
4425f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams
4435f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                rsc->timerSet(RS_TIMER_CLEAR_SWAP);
4445f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                rsc->mHal.funcs.swap(rsc);
4455f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                rsc->timerFrame();
4465f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                rsc->timerSet(RS_TIMER_INTERNAL);
4475f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                rsc->timerPrint();
4485f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams                rsc->timerReset();
4495f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            }
45083c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        }
45193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
45283c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    }
45383c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
45420f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("%p RS Thread exiting", rsc);
45583c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
45693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
45783c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    if (rsc->mIsGraphicsContext) {
45883c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        pthread_mutex_lock(&gInitMutex);
45983c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        rsc->deinitEGL();
46083c451a4ef4388e002482e383d488ca9b7b7600dJason Sams        pthread_mutex_unlock(&gInitMutex);
46183c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    }
46293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
46383c451a4ef4388e002482e383d488ca9b7b7600dJason Sams
46420f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("%p RS Thread exited", rsc);
46583c451a4ef4388e002482e383d488ca9b7b7600dJason Sams    return NULL;
466326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
467326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
468741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Samsvoid Context::destroyWorkerThreadResources() {
4696598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("destroyWorkerThreadResources 1");
4702e8665de7c0eb4514c67baf8693d61c892e5303dJason Sams    ObjectBase::zeroAllUserRef(this);
47193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
472741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams    if (mIsGraphicsContext) {
473741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mRaster.clear();
474741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mFragment.clear();
475741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mVertex.clear();
476741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mFragmentStore.clear();
477741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mFont.clear();
478741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mRootScript.clear();
479741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mStateRaster.deinit(this);
480741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mStateVertex.deinit(this);
481741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mStateFragment.deinit(this);
482741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mStateFragmentStore.deinit(this);
483741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams         mStateFont.deinit(this);
484c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk         mStateSampler.deinit(this);
485da6d34a5a6ece8c30d20673b9b6ff07d8c91768bAlex Sakhartchouk         mFBOCache.deinit(this);
486741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams    }
48793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
488c7cec1e3577cc77a5a73d5bd5a82733b1b9936a1Jason Sams    ObjectBase::freeAllChildren(this);
489cf912de17f1e086ccea707d8607a3d2eda56b98fJason Sams    mExit = true;
4905f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams    //ALOGV("destroyWorkerThreadResources 2");
491741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams}
492741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams
4932382aba4a55c6ae74789c478eead8fbd96593321Jason Samsvoid Context::printWatchdogInfo(void *ctx) {
4942382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    Context *rsc = (Context *)ctx;
495ee803446857b14d97f3e90a4f530604b6be9c867Jason Sams    if (rsc->watchdog.command && rsc->watchdog.file) {
496af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("RS watchdog timeout: %i  %s  line %i %s", rsc->watchdog.inRoot,
497ee803446857b14d97f3e90a4f530604b6be9c867Jason Sams             rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
498ee803446857b14d97f3e90a4f530604b6be9c867Jason Sams    } else {
499af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
500ee803446857b14d97f3e90a4f530604b6be9c867Jason Sams    }
5012382aba4a55c6ae74789c478eead8fbd96593321Jason Sams}
5022382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
5032382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
504afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::setPriority(int32_t p) {
505158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    // Note: If we put this in the proper "background" policy
506158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    // the wallpapers can become completly unresponsive at times.
507158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    // This is probably not what we want for something the user is actively
508158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    // looking at.
5092dca84dd6c07992f78ad050177975f16486dd77eJason Sams    mThreadPriority = p;
5107bf29ddc35450d8064541c42c99a1f48be6cf0ddJason Sams    setpriority(PRIO_PROCESS, mNativeThreadId, p);
5119719bd4a0187c400ba868712612fe66da4635aacJason Sams    mHal.funcs.setPriority(this, mThreadPriority);
512158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams}
513158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams
514afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukContext::Context() {
5155c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    mDev = NULL;
516326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    mRunning = false;
517326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    mExit = false;
51886f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    mPaused = false;
519e514b45de8561fbc6ef6770845102ca10b0a69d7Jason Sams    mObjHead = NULL;
520a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams    mError = RS_ERROR_NONE;
521cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    mTargetSdkVersion = 14;
5227b3e9bd825901e33661e3c385e3e7c6f40ca6000Alex Sakhartchouk    mDPI = 96;
523b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    mIsContextLite = false;
52486c6b5fecb3e4e68668fdb5640770545501e778fStephen Hines    memset(&watchdog, 0, sizeof(watchdog));
525f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams    memset(&mHal, 0, sizeof(mHal));
5260e92fa3775b3537c29a7a0ca603d892548936038Tim Murray    mForceCpu = false;
527cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    mContextType = RS_CONTEXT_TYPE_NORMAL;
5284d252d6e807b89764dad123ac845df298c52ca97Tim Murray    mSynchronous = false;
5290e92fa3775b3537c29a7a0ca603d892548936038Tim Murray}
5300e92fa3775b3537c29a7a0ca603d892548936038Tim Murray
5310e92fa3775b3537c29a7a0ca603d892548936038Tim MurrayContext * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
53284e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray                                 RsContextType ct, uint32_t flags) {
5335c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    Context * rsc = new Context();
534b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk
53584e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray    if (flags & RS_CONTEXT_LOW_LATENCY) {
53684e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray        rsc->mForceCpu = true;
53784e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray    }
53884e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray    if (flags & RS_CONTEXT_SYNCHRONOUS) {
53984e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray        rsc->mSynchronous = true;
54084e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray    }
541cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    rsc->mContextType = ct;
542416f4b6d3ee6f30b747e7cd556b28d84b0226cf3Jason Sams    rsc->mHal.flags = flags;
5430e92fa3775b3537c29a7a0ca603d892548936038Tim Murray
5445c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    if (!rsc->initContext(dev, sc)) {
5455c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams        delete rsc;
5465c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams        return NULL;
5475c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    }
5485c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    return rsc;
5495c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams}
5505c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams
551b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex SakhartchoukContext * Context::createContextLite() {
552b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    Context * rsc = new Context();
553b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    rsc->mIsContextLite = true;
554b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    return rsc;
555b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk}
556b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk
557afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukbool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
5585c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    pthread_mutex_lock(&gInitMutex);
5595c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams
5601a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    mIO.init();
5615f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams    mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
5621a4efa363916977ef9aeab756725b3bdc880a15bJason Sams
5635c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    dev->addContext(this);
5645c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    mDev = dev;
5656b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    if (sc) {
5666b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams        mUserSurfaceConfig = *sc;
5676b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    } else {
5686b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams        memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
5696b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    }
570a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams
5716b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    mIsGraphicsContext = sc != NULL;
572326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
573a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams    int status;
574a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams    pthread_attr_t threadAttr;
575a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams
576fb03a22ad2adadc1ff50a8b50d43ad7fcc3fa6edJason Sams    pthread_mutex_unlock(&gInitMutex);
577fb03a22ad2adadc1ff50a8b50d43ad7fcc3fa6edJason Sams
578fb03a22ad2adadc1ff50a8b50d43ad7fcc3fa6edJason Sams    // Global init done at this point.
579e57691037aea219562ac686429b4b98202aab7bcJason Sams
580a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams    status = pthread_attr_init(&threadAttr);
581a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams    if (status) {
582af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Failed to init thread attribute.");
5835c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams        return false;
584a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams    }
585a658e905f14fe500bfee3812b213a73cb37ef6f2Jason Sams
5867257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk    mHasSurface = false;
587992a0b70d8fd7a14f0c57bc3c7e16c1f269a6609Jason Sams
58824371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    timerInit();
589a891933b4c5ab1b63103add167269cfc404c2adfJason Sams    timerSet(RS_TIMER_INTERNAL);
5904d252d6e807b89764dad123ac845df298c52ca97Tim Murray    if (mSynchronous) {
5914d252d6e807b89764dad123ac845df298c52ca97Tim Murray        threadProc(this);
592fea96e8d145b1915b7bd66f68e973dd572a469e3Xiaofei Wan
593fea96e8d145b1915b7bd66f68e973dd572a469e3Xiaofei Wan        if (mError != RS_ERROR_NONE) {
594fea96e8d145b1915b7bd66f68e973dd572a469e3Xiaofei Wan            ALOGE("Errors during thread init (sync mode)");
595fea96e8d145b1915b7bd66f68e973dd572a469e3Xiaofei Wan            return false;
596fea96e8d145b1915b7bd66f68e973dd572a469e3Xiaofei Wan        }
5974d252d6e807b89764dad123ac845df298c52ca97Tim Murray    } else {
5984d252d6e807b89764dad123ac845df298c52ca97Tim Murray        status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
5994d252d6e807b89764dad123ac845df298c52ca97Tim Murray        if (status) {
6004d252d6e807b89764dad123ac845df298c52ca97Tim Murray            ALOGE("Failed to start rs context thread.");
6014d252d6e807b89764dad123ac845df298c52ca97Tim Murray            return false;
6024d252d6e807b89764dad123ac845df298c52ca97Tim Murray        }
6034d252d6e807b89764dad123ac845df298c52ca97Tim Murray        while (!mRunning && (mError == RS_ERROR_NONE)) {
6044d252d6e807b89764dad123ac845df298c52ca97Tim Murray            usleep(100);
6054d252d6e807b89764dad123ac845df298c52ca97Tim Murray        }
6065086938044e0a9b6b1138f915d0d252fe046e102Jason Sams
6074d252d6e807b89764dad123ac845df298c52ca97Tim Murray        if (mError != RS_ERROR_NONE) {
6084d252d6e807b89764dad123ac845df298c52ca97Tim Murray            ALOGE("Errors during thread init");
6094d252d6e807b89764dad123ac845df298c52ca97Tim Murray            return false;
6104d252d6e807b89764dad123ac845df298c52ca97Tim Murray        }
611181334092f78273d0da2d854ebbe7b24508d3bfcJason Sams
6124d252d6e807b89764dad123ac845df298c52ca97Tim Murray        pthread_attr_destroy(&threadAttr);
6135c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    }
6145c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    return true;
615326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
616326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
617afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukContext::~Context() {
61820f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("%p Context::~Context", this);
619cf912de17f1e086ccea707d8607a3d2eda56b98fJason Sams
620b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    if (!mIsContextLite) {
621b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        mPaused = false;
622b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        void *res;
623326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
624b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        mIO.shutdown();
625bcba6f666e43c9af8d7f28e581f1cf4badb0dcdfSangkyu Lee        if (!mSynchronous) {
626bcba6f666e43c9af8d7f28e581f1cf4badb0dcdfSangkyu Lee            pthread_join(mThreadId, &res);
627bcba6f666e43c9af8d7f28e581f1cf4badb0dcdfSangkyu Lee        }
6285f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams        rsAssert(mExit);
629326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
630fea96e8d145b1915b7bd66f68e973dd572a469e3Xiaofei Wan        if (mHal.funcs.shutdownDriver && mHal.drv) {
631b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk            mHal.funcs.shutdownDriver(this);
632b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        }
633249d453869cca82dcb57123f6d1c8df3ca9f4372Jason Sams
634b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        // Global structure cleanup.
635b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        pthread_mutex_lock(&gInitMutex);
636b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        if (mDev) {
637b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk            mDev->removeContext(this);
638b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk            mDev = NULL;
639b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        }
640b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk        pthread_mutex_unlock(&gInitMutex);
641326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    }
64220f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("%p Context::~Context done", this);
643326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
644326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
64593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
6467257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchoukvoid Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
6474820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
6484b3de47071d875faaa7d419d050a464b09538797Jason Sams    mHal.funcs.setSurface(this, w, h, sur);
649458f2dc26b7d34c2138c7bfbd95914240084e6bdJason Sams
6507257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk    mHasSurface = sur != NULL;
6514b3de47071d875faaa7d419d050a464b09538797Jason Sams    mWidth = w;
6524b3de47071d875faaa7d419d050a464b09538797Jason Sams    mHeight = h;
653613cad1702dbb76eb2a6ba0cfcb43b9fe207cebcJason Sams
6544b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (mWidth && mHeight) {
655771565f47fc44608444c00aa8fa3bda769ceaeceJason Sams        mStateVertex.updateSize(this);
656a544b6368d7ddae47985da92d6bcf86798b376c6Alex Sakhartchouk        mFBOCache.updateSize();
657458f2dc26b7d34c2138c7bfbd95914240084e6bdJason Sams    }
658458f2dc26b7d34c2138c7bfbd95914240084e6bdJason Sams}
659458f2dc26b7d34c2138c7bfbd95914240084e6bdJason Sams
660a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchoukuint32_t Context::getCurrentSurfaceWidth() const {
661a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
662a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk        if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
663a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk            return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
664a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk        }
665a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    }
666a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    if (mFBOCache.mHal.state.depthTarget != NULL) {
667a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk        return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
668a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    }
669a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    return mWidth;
670a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk}
671a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk
672a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchoukuint32_t Context::getCurrentSurfaceHeight() const {
673a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
674a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk        if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
675a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk            return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
676a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk        }
677a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    }
678a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    if (mFBOCache.mHal.state.depthTarget != NULL) {
679a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk        return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
680a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    }
681a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    return mHeight;
682a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk}
683a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk
684afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::pause() {
6854820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
68686f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    mPaused = true;
68786f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams}
68886f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams
689afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::resume() {
6904820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
69186f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    mPaused = false;
69286f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams}
69386f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams
694afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::setRootScript(Script *s) {
6954820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
696326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    mRootScript.set(s);
697326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
698326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
69960709257bbdeb0c50f39b9c8969dc76264d6e142Jason Samsvoid Context::setProgramStore(ProgramStore *pfs) {
7004820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
7018ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    if (pfs == NULL) {
7028ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        mFragmentStore.set(mStateFragmentStore.mDefault);
7038ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    } else {
7048ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        mFragmentStore.set(pfs);
7058ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
706326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
707326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
70860709257bbdeb0c50f39b9c8969dc76264d6e142Jason Samsvoid Context::setProgramFragment(ProgramFragment *pf) {
7094820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
7108ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    if (pf == NULL) {
7118ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        mFragment.set(mStateFragment.mDefault);
7128ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    } else {
7138ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        mFragment.set(pf);
7148ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
715cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams}
716cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams
71760709257bbdeb0c50f39b9c8969dc76264d6e142Jason Samsvoid Context::setProgramRaster(ProgramRaster *pr) {
7184820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
7195fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    if (pr == NULL) {
7205fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams        mRaster.set(mStateRaster.mDefault);
7215fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    } else {
7225fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams        mRaster.set(pr);
7235fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    }
7245fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams}
7255fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams
72660709257bbdeb0c50f39b9c8969dc76264d6e142Jason Samsvoid Context::setProgramVertex(ProgramVertex *pv) {
7274820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    rsAssert(mIsGraphicsContext);
7288ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    if (pv == NULL) {
7298ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        mVertex.set(mStateVertex.mDefault);
7308ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    } else {
7318ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        mVertex.set(pv);
7328ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
733326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
734326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
735afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::setFont(Font *f) {
736d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    rsAssert(mIsGraphicsContext);
737d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    if (f == NULL) {
738d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk        mFont.set(mStateFont.mDefault);
739d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    } else {
740d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk        mFont.set(f);
741d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    }
742d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk}
74393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
744d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk
745f2748276ea84bed83728e38a6661946b5fb3bc5cJason Samsvoid Context::finish() {
746f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams    if (mHal.funcs.finish) {
747f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams        mHal.funcs.finish(this);
748f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams    }
749f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams}
750f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams
751afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
752a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    rsAssert(!obj->getName());
753a4a54e42fc710a62b47cbcb9d64c34a190429d9eJason Sams    obj->setName(name, len);
754a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    mNames.add(obj);
755a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams}
756a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams
757afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::removeName(ObjectBase *obj) {
758afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    for (size_t ct=0; ct < mNames.size(); ct++) {
759a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams        if (obj == mNames[ct]) {
760a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams            mNames.removeAt(ct);
761a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams            return;
762a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams        }
763a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    }
764a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams}
765a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams
7661a4efa363916977ef9aeab756725b3bdc880a15bJason SamsRsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
7671a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
768aad4bc5231dd7059fc5148b34a951117d9b5f4adJason Sams}
769aad4bc5231dd7059fc5148b34a951117d9b5f4adJason Sams
7701a4efa363916977ef9aeab756725b3bdc880a15bJason SamsRsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
7711a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
7728c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
7738c401effb0837155fc39ca0364f57a882d127d38Jason Sams
77487319de2b16a185cf360827c96a42cf1fcaae744Jason Samsbool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
77587319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                                  uint32_t subID, size_t len, bool waitForSpace) const {
7761a4efa363916977ef9aeab756725b3bdc880a15bJason Sams
7774961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams    pthread_mutex_lock(&gMessageMutex);
7784961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams    bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
7794961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams    pthread_mutex_unlock(&gMessageMutex);
7804961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams    return ret;
7818c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
7828c401effb0837155fc39ca0364f57a882d127d38Jason Sams
783afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::initToClient() {
784afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    while (!mRunning) {
7858c401effb0837155fc39ca0364f57a882d127d38Jason Sams        usleep(100);
7868c401effb0837155fc39ca0364f57a882d127d38Jason Sams    }
7878c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
7888c401effb0837155fc39ca0364f57a882d127d38Jason Sams
789afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::deinitToClient() {
7901a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    mIO.clientShutdown();
7918c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
7925086938044e0a9b6b1138f915d0d252fe046e102Jason Sams
79387319de2b16a185cf360827c96a42cf1fcaae744Jason Samsvoid Context::setError(RsError e, const char *msg) const {
794a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams    mError = e;
795aad4bc5231dd7059fc5148b34a951117d9b5f4adJason Sams    sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
796a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams}
797a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams
798a2cf755a28a1e7ffff2955df656d714f40e4d715Jason Sams
799afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Context::dumpDebug() const {
800af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE("RS Context debug %p", this);
801af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE("RS Context debug");
80213e2634a71a30d289ed8d821aef61c7d1687460eJason Sams
803af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" RS width %i, height %i", mWidth, mHeight);
804af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
805af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
80613e2634a71a30d289ed8d821aef61c7d1687460eJason Sams}
807a4a54e42fc710a62b47cbcb9d64c34a190429d9eJason Sams
808326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams///////////////////////////////////////////////////////////////////////////////////////////
809a44cb29164726cd9d812117819abdd7b60dfdd93Jason Sams//
810326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
811326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
812326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
813326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
814afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextFinish(Context *rsc) {
815f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams    rsc->finish();
8168c88090e8cccab103eebe2ff569e116e9f5fb208Jason Sams}
817326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
818afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
81993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
820326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    Script *s = static_cast<Script *>(vs);
821326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    rsc->setRootScript(s);
82293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
823326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
824326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
825afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
826326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    Sampler *s = static_cast<Sampler *>(vs);
827326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
828326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    if (slot > RS_MAX_SAMPLER_SLOT) {
829af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Invalid sampler slot");
830326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams        return;
831326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    }
832326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
833326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    s->bindToContext(&rsc->mStateSampler, slot);
834326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
835326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
83693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
837afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
838ccc010bb7c0f89e162bf60033968a20be90a903aJason Sams    ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
83960709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    rsc->setProgramStore(pfs);
840326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
841326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
842afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
843326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
84460709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    rsc->setProgramFragment(pf);
845326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
846326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
847afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
8485fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
84960709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    rsc->setProgramRaster(pr);
8505fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams}
8515fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams
852afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
853326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
85460709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    rsc->setProgramVertex(pv);
855326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
856326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
857afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextBindFont(Context *rsc, RsFont vfont) {
858d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    Font *font = static_cast<Font *>(vfont);
859d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    rsc->setFont(font);
860d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk}
86193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
862d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk
863b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchoukvoid rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
864a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    ObjectBase *ob = static_cast<ObjectBase *>(obj);
86570b83c111beceaf8fbb700580833e7fec99272cfAlex Sakhartchouk    rsc->assignName(ob, name, name_length);
866a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams}
867326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
868afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ObjDestroy(Context *rsc, void *optr) {
8692353ae303868d04e3a26002b2f2dc456c15e8170Jason Sams    ObjectBase *ob = static_cast<ObjectBase *>(optr);
870707aaf341a4b068e6ccadf923af85acdd85fd775Jason Sams    rsc->removeName(ob);
8719397e30ce5fe3f6af9212a93b490836b04fdfffaJason Sams    ob->decUserRef();
872707aaf341a4b068e6ccadf923af85acdd85fd775Jason Sams}
873707aaf341a4b068e6ccadf923af85acdd85fd775Jason Sams
87493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
875afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextPause(Context *rsc) {
87686f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    rsc->pause();
87786f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams}
87886f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams
879afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextResume(Context *rsc) {
88086f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    rsc->resume();
88186f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams}
88286f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams
8837257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchoukvoid rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
884fa4028663712dbb1a3d13c507c3bc13c6e4be80dMathias Agopian    rsc->setSurface(w, h, sur);
885613cad1702dbb76eb2a6ba0cfcb43b9fe207cebcJason Sams}
88693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
887613cad1702dbb76eb2a6ba0cfcb43b9fe207cebcJason Sams
888afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextSetPriority(Context *rsc, int32_t p) {
889158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    rsc->setPriority(p);
890458f2dc26b7d34c2138c7bfbd95914240084e6bdJason Sams}
891458f2dc26b7d34c2138c7bfbd95914240084e6bdJason Sams
892afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsi_ContextDump(Context *rsc, int32_t bits) {
893c21cf40f6ae69091bf24f87b5eeabc95e73dd271Jason Sams    ObjectBase::dumpAll(rsc);
894c21cf40f6ae69091bf24f87b5eeabc95e73dd271Jason Sams}
895c21cf40f6ae69091bf24f87b5eeabc95e73dd271Jason Sams
896741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Samsvoid rsi_ContextDestroyWorker(Context *rsc) {
897b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    rsc->destroyWorkerThreadResources();
8981dcefab7178f0644b0e0a6998007b1acae5261cfJason Sams}
8991dcefab7178f0644b0e0a6998007b1acae5261cfJason Sams
900c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason Samsvoid rsi_ContextDestroy(Context *rsc) {
901a36c50a6ab87f4c9049318d4c6c8ec7b0a1e6e12Jason Sams    //ALOGE("%p rsContextDestroy", rsc);
902741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams    rsContextDestroyWorker(rsc);
903741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams    delete rsc;
90420f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("%p rsContextDestroy done", rsc);
905741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams}
906326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
907c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason SamsRsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
908186e59154a0fb86e05e283e9c083b69878c06720Jason Sams                                           size_t * receiveLen, size_t receiveLen_length,
9091a4efa363916977ef9aeab756725b3bdc880a15bJason Sams                                           uint32_t * subID, size_t subID_length) {
9101a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    return rsc->peekMessageToClient(receiveLen, subID);
911aad4bc5231dd7059fc5148b34a951117d9b5f4adJason Sams}
912aad4bc5231dd7059fc5148b34a951117d9b5f4adJason Sams
913c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason SamsRsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
914186e59154a0fb86e05e283e9c083b69878c06720Jason Sams                                          size_t * receiveLen, size_t receiveLen_length,
9151a4efa363916977ef9aeab756725b3bdc880a15bJason Sams                                          uint32_t * subID, size_t subID_length) {
916186e59154a0fb86e05e283e9c083b69878c06720Jason Sams    rsAssert(subID_length == sizeof(uint32_t));
917186e59154a0fb86e05e283e9c083b69878c06720Jason Sams    rsAssert(receiveLen_length == sizeof(size_t));
9181a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    return rsc->getMessageToClient(data, receiveLen, subID, data_length);
9198c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
9208c401effb0837155fc39ca0364f57a882d127d38Jason Sams
921c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason Samsvoid rsi_ContextInitToClient(Context *rsc) {
9228c401effb0837155fc39ca0364f57a882d127d38Jason Sams    rsc->initToClient();
9238c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
9248c401effb0837155fc39ca0364f57a882d127d38Jason Sams
925c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason Samsvoid rsi_ContextDeinitToClient(Context *rsc) {
9268c401effb0837155fc39ca0364f57a882d127d38Jason Sams    rsc->deinitToClient();
9278c401effb0837155fc39ca0364f57a882d127d38Jason Sams}
9288c401effb0837155fc39ca0364f57a882d127d38Jason Sams
929702652065131e740c04e0744bb433028e15d8434Jason Samsvoid rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
930702652065131e740c04e0744bb433028e15d8434Jason Sams    rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
931702652065131e740c04e0744bb433028e15d8434Jason Sams}
932702652065131e740c04e0744bb433028e15d8434Jason Sams
933397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray// implementation of handcode LF_ObjDestroy
934397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray// required so nObjDestroy can be run from finalizer without blocking
935397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murrayvoid LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) {
936397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    if (((Context *)rsc)->isSynchronous()) {
937397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray        rsi_ObjDestroy((Context *)rsc, objPtr);
938397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray        return;
939397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    }
940397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray
941397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    // struct has two parts:
942397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    // RsPlaybackRemoteHeader (cmdID and bytes)
943397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    // RS_CMD_ObjDestroy (ptr)
944397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    struct destroyCmd {
945397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray        uint32_t cmdID;
946397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray        uint32_t bytes;
947397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray        RsAsyncVoidPtr ptr;
948397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray     };
949397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray
950397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    destroyCmd cmd;
951397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    cmd.cmdID = RS_CMD_ID_ObjDestroy;
952397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    cmd.bytes = sizeof(RsAsyncVoidPtr);
953397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    cmd.ptr = objPtr;
954397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    ThreadIO *io = &((Context *)rsc)->mIO;
955397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray    io->coreWrite((void*)&cmd, sizeof(destroyCmd));
956397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray
957397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray}
958397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray
959c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason Sams}
960c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason Sams}
961c975cf4a71b63ccbd20f1f3b3341c5f2e6025b45Jason Sams
962c2ce707a3d1a8eae79bcf1c749afc6d6e7969ad9Tim Murrayextern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
96384e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray                                     RsContextType ct, uint32_t flags) {
96420f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("rsContextCreate dev=%p", vdev);
965789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams    Device * dev = static_cast<Device *>(vdev);
96684e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray    Context *rsc = Context::createContext(dev, NULL, ct, flags);
967cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    if (rsc) {
968cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines        rsc->setTargetSdkVersion(sdkVersion);
969cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    }
970789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams    return rsc;
971789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams}
972789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams
97393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
974789ca83c794cb7196c9bac97d39eaf1f7947af2aJason SamsRsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
975cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines                            uint32_t sdkVersion, RsSurfaceConfig sc,
976cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines                            uint32_t dpi) {
97720f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("rsContextCreateGL dev=%p", vdev);
978789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams    Device * dev = static_cast<Device *>(vdev);
979789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams    Context *rsc = Context::createContext(dev, &sc);
9809544f76195de22f655fd7a1894934667e7e55f6aJason Sams    if (rsc) {
981cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines        rsc->setTargetSdkVersion(sdkVersion);
9829544f76195de22f655fd7a1894934667e7e55f6aJason Sams        rsc->setDPI(dpi);
9839544f76195de22f655fd7a1894934667e7e55f6aJason Sams    }
98420f62caa884739810a0074383952797abb9b08a3Tim Murray    //ALOGV("%p rsContextCreateGL ret", rsc);
985789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams    return rsc;
986789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams}
98793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
988789ca83c794cb7196c9bac97d39eaf1f7947af2aJason Sams
989dc763f345db3e796efc28dc4b4d8edffda5a803eAlex Sakhartchouk// Only to be called at a3d load time, before object is visible to user
990dc763f345db3e796efc28dc4b4d8edffda5a803eAlex Sakhartchouk// not thread safe
991afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid rsaGetName(RsContext con, void *