rsFBOCache.cpp revision da6d34a5a6ece8c30d20673b9b6ff07d8c91768b
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsFBOCache.h"
18
19#include "rsContext.h"
20#include "rsAllocation.h"
21
22using namespace android;
23using namespace android::renderscript;
24
25
26FBOCache::FBOCache() {
27    mDirty = true;
28    mHal.state.colorTargetsCount = 1;
29    mHal.state.colorTargets = new ObjectBaseRef<Allocation>[mHal.state.colorTargetsCount];
30}
31
32FBOCache::~FBOCache() {
33    delete[] mHal.state.colorTargets;
34}
35
36void FBOCache::init(Context *rsc) {
37    rsc->mHal.funcs.framebuffer.init(rsc, this);
38}
39
40void FBOCache::deinit(Context *rsc) {
41    rsc->mHal.funcs.framebuffer.destroy(rsc, this);
42}
43
44void FBOCache::bindColorTarget(Context *rsc, Allocation *a, uint32_t slot) {
45    if (slot >= mHal.state.colorTargetsCount) {
46        LOGE("Invalid render target index");
47        return;
48    }
49    if (a != NULL) {
50        if (!a->getIsTexture()) {
51            LOGE("Invalid Color Target");
52            return;
53        }
54        if (a->getIsTexture()) {
55            if (a->getTextureID() == 0) {
56                a->deferredUploadToTexture(rsc);
57            }
58        } else if (a->getRenderTargetID() == 0) {
59            a->deferredAllocateRenderTarget(rsc);
60        }
61    }
62    mHal.state.colorTargets[slot].set(a);
63    mDirty = true;
64}
65
66void FBOCache::bindDepthTarget(Context *rsc, Allocation *a) {
67    if (a != NULL) {
68        if (!a->getIsRenderTarget()) {
69            LOGE("Invalid Depth Target");
70            return;
71        }
72        if (a->getIsTexture()) {
73            if (a->getTextureID() == 0) {
74                a->deferredUploadToTexture(rsc);
75            }
76        } else if (a->getRenderTargetID() == 0) {
77            a->deferredAllocateRenderTarget(rsc);
78        }
79    }
80    mHal.state.depthTarget.set(a);
81    mDirty = true;
82}
83
84void FBOCache::resetAll(Context *) {
85    for (uint32_t i = 0; i < mHal.state.colorTargetsCount; i ++) {
86        mHal.state.colorTargets[i].set(NULL);
87    }
88    mHal.state.depthTarget.set(NULL);
89    mDirty = true;
90}
91
92void FBOCache::setup(Context *rsc) {
93    if (!mDirty) {
94        return;
95    }
96
97    if (mHal.state.depthTarget.get() != NULL) {
98        mHal.state.depthTarget->uploadCheck(rsc);
99    }
100
101    for (uint32_t i = 0; i < mHal.state.colorTargetsCount; i ++) {
102        if (mHal.state.colorTargets[i].get() != NULL) {
103            mHal.state.colorTargets[i]->uploadCheck(rsc);
104        }
105    }
106
107    rsc->mHal.funcs.framebuffer.setActive(rsc, this);
108
109    mDirty = false;
110}
111