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 Allocation*[mHal.state.colorTargetsCount];
30    mColorTargets = new ObjectBaseRef<Allocation>[mHal.state.colorTargetsCount];
31    resetAll(nullptr);
32}
33
34FBOCache::~FBOCache() {
35    delete[] mHal.state.colorTargets;
36    delete[] mColorTargets;
37}
38
39void FBOCache::init(Context *rsc) {
40    rsc->mHal.funcs.framebuffer.init(rsc, this);
41}
42
43void FBOCache::deinit(Context *rsc) {
44    rsc->mHal.funcs.framebuffer.destroy(rsc, this);
45}
46
47void FBOCache::bindColorTarget(Context *rsc, Allocation *a, uint32_t slot) {
48    if (slot >= mHal.state.colorTargetsCount) {
49        ALOGE("Invalid render target index");
50        return;
51    }
52    if (a != nullptr) {
53        if (!(a->getIsTexture() || (a->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT))) {
54            ALOGE("Invalid Color Target");
55            return;
56        }
57    }
58    mColorTargets[slot].set(a);
59    mHal.state.colorTargets[slot] = a;
60    mDirty = true;
61}
62
63void FBOCache::bindDepthTarget(Context *rsc, Allocation *a) {
64    if (a != nullptr) {
65        if (!a->getIsRenderTarget()) {
66            ALOGE("Invalid Depth Target");
67            return;
68        }
69    }
70    mDepthTarget.set(a);
71    mHal.state.depthTarget = a;
72    mDirty = true;
73}
74
75void FBOCache::resetAll(Context *) {
76    for (uint32_t i = 0; i < mHal.state.colorTargetsCount; i ++) {
77        mColorTargets[i].set(nullptr);
78        mHal.state.colorTargets[i] = nullptr;
79    }
80    mDepthTarget.set(nullptr);
81    mHal.state.depthTarget = nullptr;
82    mDirty = true;
83}
84
85void FBOCache::setup(Context *rsc) {
86    if (!mDirty) {
87        return;
88    }
89
90    rsc->mHal.funcs.framebuffer.setActive(rsc, this);
91
92    mDirty = false;
93}
94