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