19fded232a9548a304e0145011df8849fba0dcda7Chris Craik/*
29fded232a9548a304e0145011df8849fba0dcda7Chris Craik * Copyright (C) 2015 The Android Open Source Project
39fded232a9548a304e0145011df8849fba0dcda7Chris Craik *
49fded232a9548a304e0145011df8849fba0dcda7Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
59fded232a9548a304e0145011df8849fba0dcda7Chris Craik * you may not use this file except in compliance with the License.
69fded232a9548a304e0145011df8849fba0dcda7Chris Craik * You may obtain a copy of the License at
79fded232a9548a304e0145011df8849fba0dcda7Chris Craik *
89fded232a9548a304e0145011df8849fba0dcda7Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
99fded232a9548a304e0145011df8849fba0dcda7Chris Craik *
109fded232a9548a304e0145011df8849fba0dcda7Chris Craik * Unless required by applicable law or agreed to in writing, software
119fded232a9548a304e0145011df8849fba0dcda7Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
129fded232a9548a304e0145011df8849fba0dcda7Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139fded232a9548a304e0145011df8849fba0dcda7Chris Craik * See the License for the specific language governing permissions and
149fded232a9548a304e0145011df8849fba0dcda7Chris Craik * limitations under the License.
159fded232a9548a304e0145011df8849fba0dcda7Chris Craik */
169fded232a9548a304e0145011df8849fba0dcda7Chris Craik
179fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "OffscreenBufferPool.h"
189fded232a9548a304e0145011df8849fba0dcda7Chris Craik
199fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "Caches.h"
209fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "Properties.h"
219fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "renderstate/RenderState.h"
229fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "utils/FatVector.h"
23aff230f6f9f9e44a5e111ba3f087d03f7a0e24f3Chris Craik#include "utils/TraceUtils.h"
249fded232a9548a304e0145011df8849fba0dcda7Chris Craik
259fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include <utils/Log.h>
269fded232a9548a304e0145011df8849fba0dcda7Chris Craik
279fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include <GLES2/gl2.h>
289fded232a9548a304e0145011df8849fba0dcda7Chris Craik
299fded232a9548a304e0145011df8849fba0dcda7Chris Craiknamespace android {
309fded232a9548a304e0145011df8849fba0dcda7Chris Craiknamespace uirenderer {
319fded232a9548a304e0145011df8849fba0dcda7Chris Craik
329fded232a9548a304e0145011df8849fba0dcda7Chris Craik////////////////////////////////////////////////////////////////////////////////
339fded232a9548a304e0145011df8849fba0dcda7Chris Craik// OffscreenBuffer
349fded232a9548a304e0145011df8849fba0dcda7Chris Craik////////////////////////////////////////////////////////////////////////////////
359fded232a9548a304e0145011df8849fba0dcda7Chris Craik
369fded232a9548a304e0145011df8849fba0dcda7Chris CraikOffscreenBuffer::OffscreenBuffer(RenderState& renderState, Caches& caches,
379fded232a9548a304e0145011df8849fba0dcda7Chris Craik        uint32_t viewportWidth, uint32_t viewportHeight)
3838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        : GpuMemoryTracker(GpuObjectType::OffscreenBuffer)
3938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        , renderState(renderState)
409fded232a9548a304e0145011df8849fba0dcda7Chris Craik        , viewportWidth(viewportWidth)
419fded232a9548a304e0145011df8849fba0dcda7Chris Craik        , viewportHeight(viewportHeight)
429fded232a9548a304e0145011df8849fba0dcda7Chris Craik        , texture(caches) {
4338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    uint32_t width = computeIdealDimension(viewportWidth);
4438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    uint32_t height = computeIdealDimension(viewportHeight);
45aff230f6f9f9e44a5e111ba3f087d03f7a0e24f3Chris Craik    ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height);
46bd41ded59090e8cdf598e0c65693f1cf0885bbeaJohn Reck    caches.textureState().activateTexture(0);
4738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    texture.resize(width, height, GL_RGBA);
489fded232a9548a304e0145011df8849fba0dcda7Chris Craik    texture.blend = true;
4938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    texture.setWrap(GL_CLAMP_TO_EDGE);
509fded232a9548a304e0145011df8849fba0dcda7Chris Craik    // not setting filter on texture, since it's set when drawing, based on transform
519fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
529fded232a9548a304e0145011df8849fba0dcda7Chris Craik
537435eb148e72382126e9073183e881357bb38a8bChris CraikRect OffscreenBuffer::getTextureCoordinates() {
5438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    const float texX = 1.0f / static_cast<float>(texture.width());
5538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    const float texY = 1.0f / static_cast<float>(texture.height());
567435eb148e72382126e9073183e881357bb38a8bChris Craik    return Rect(0, viewportHeight * texY, viewportWidth * texX, 0);
577435eb148e72382126e9073183e881357bb38a8bChris Craik}
587435eb148e72382126e9073183e881357bb38a8bChris Craik
5964db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craikvoid OffscreenBuffer::dirty(Rect dirtyArea) {
6064db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik    dirtyArea.doIntersect(0, 0, viewportWidth, viewportHeight);
6164db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik    if (!dirtyArea.isEmpty()) {
6264db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik        region.orSelf(android::Rect(dirtyArea.left, dirtyArea.top,
6364db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik                dirtyArea.right, dirtyArea.bottom));
6464db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik    }
6564db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik}
6664db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik
679fded232a9548a304e0145011df8849fba0dcda7Chris Craikvoid OffscreenBuffer::updateMeshFromRegion() {
689fded232a9548a304e0145011df8849fba0dcda7Chris Craik    // avoid T-junctions as they cause artifacts in between the resultant
699fded232a9548a304e0145011df8849fba0dcda7Chris Craik    // geometry when complex transforms occur.
709fded232a9548a304e0145011df8849fba0dcda7Chris Craik    // TODO: generate the safeRegion only if necessary based on drawing transform
719fded232a9548a304e0145011df8849fba0dcda7Chris Craik    Region safeRegion = Region::createTJunctionFreeRegion(region);
729fded232a9548a304e0145011df8849fba0dcda7Chris Craik
739fded232a9548a304e0145011df8849fba0dcda7Chris Craik    size_t count;
749fded232a9548a304e0145011df8849fba0dcda7Chris Craik    const android::Rect* rects = safeRegion.getArray(&count);
759fded232a9548a304e0145011df8849fba0dcda7Chris Craik
7638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    const float texX = 1.0f / float(texture.width());
7738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    const float texY = 1.0f / float(texture.height());
789fded232a9548a304e0145011df8849fba0dcda7Chris Craik
799fded232a9548a304e0145011df8849fba0dcda7Chris Craik    FatVector<TextureVertex, 64> meshVector(count * 4); // uses heap if more than 64 vertices needed
809fded232a9548a304e0145011df8849fba0dcda7Chris Craik    TextureVertex* mesh = &meshVector[0];
819fded232a9548a304e0145011df8849fba0dcda7Chris Craik    for (size_t i = 0; i < count; i++) {
829fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const android::Rect* r = &rects[i];
839fded232a9548a304e0145011df8849fba0dcda7Chris Craik
849fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const float u1 = r->left * texX;
859fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const float v1 = (viewportHeight - r->top) * texY;
869fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const float u2 = r->right * texX;
879fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const float v2 = (viewportHeight - r->bottom) * texY;
889fded232a9548a304e0145011df8849fba0dcda7Chris Craik
899fded232a9548a304e0145011df8849fba0dcda7Chris Craik        TextureVertex::set(mesh++, r->left, r->top, u1, v1);
909fded232a9548a304e0145011df8849fba0dcda7Chris Craik        TextureVertex::set(mesh++, r->right, r->top, u2, v1);
919fded232a9548a304e0145011df8849fba0dcda7Chris Craik        TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
929fded232a9548a304e0145011df8849fba0dcda7Chris Craik        TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
939fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
949fded232a9548a304e0145011df8849fba0dcda7Chris Craik    elementCount = count * 6;
959fded232a9548a304e0145011df8849fba0dcda7Chris Craik    renderState.meshState().genOrUpdateMeshBuffer(&vbo,
969fded232a9548a304e0145011df8849fba0dcda7Chris Craik            sizeof(TextureVertex) * count * 4,
979fded232a9548a304e0145011df8849fba0dcda7Chris Craik            &meshVector[0],
989fded232a9548a304e0145011df8849fba0dcda7Chris Craik            GL_DYNAMIC_DRAW); // TODO: GL_STATIC_DRAW if savelayer
999fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1009fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1019fded232a9548a304e0145011df8849fba0dcda7Chris Craikuint32_t OffscreenBuffer::computeIdealDimension(uint32_t dimension) {
1029fded232a9548a304e0145011df8849fba0dcda7Chris Craik    return uint32_t(ceilf(dimension / float(LAYER_SIZE)) * LAYER_SIZE);
1039fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1049fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1059fded232a9548a304e0145011df8849fba0dcda7Chris CraikOffscreenBuffer::~OffscreenBuffer() {
106aff230f6f9f9e44a5e111ba3f087d03f7a0e24f3Chris Craik    ATRACE_FORMAT("Destroy %ux%u HW Layer", texture.width(), texture.height());
1079fded232a9548a304e0145011df8849fba0dcda7Chris Craik    texture.deleteTexture();
1089fded232a9548a304e0145011df8849fba0dcda7Chris Craik    renderState.meshState().deleteMeshBuffer(vbo);
1099fded232a9548a304e0145011df8849fba0dcda7Chris Craik    elementCount = 0;
1109fded232a9548a304e0145011df8849fba0dcda7Chris Craik    vbo = 0;
1119fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1129fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1139fded232a9548a304e0145011df8849fba0dcda7Chris Craik///////////////////////////////////////////////////////////////////////////////
1149fded232a9548a304e0145011df8849fba0dcda7Chris Craik// OffscreenBufferPool
1159fded232a9548a304e0145011df8849fba0dcda7Chris Craik///////////////////////////////////////////////////////////////////////////////
1169fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1179fded232a9548a304e0145011df8849fba0dcda7Chris CraikOffscreenBufferPool::OffscreenBufferPool()
1189fded232a9548a304e0145011df8849fba0dcda7Chris Craik    : mMaxSize(Properties::layerPoolSize) {
1199fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1209fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1219fded232a9548a304e0145011df8849fba0dcda7Chris CraikOffscreenBufferPool::~OffscreenBufferPool() {
1229fded232a9548a304e0145011df8849fba0dcda7Chris Craik    clear(); // TODO: unique_ptr?
1239fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1249fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1259fded232a9548a304e0145011df8849fba0dcda7Chris Craikint OffscreenBufferPool::Entry::compare(const Entry& lhs, const Entry& rhs) {
1269fded232a9548a304e0145011df8849fba0dcda7Chris Craik    int deltaInt = int(lhs.width) - int(rhs.width);
1279fded232a9548a304e0145011df8849fba0dcda7Chris Craik    if (deltaInt != 0) return deltaInt;
1289fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1299fded232a9548a304e0145011df8849fba0dcda7Chris Craik    return int(lhs.height) - int(rhs.height);
1309fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1319fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1329fded232a9548a304e0145011df8849fba0dcda7Chris Craikvoid OffscreenBufferPool::clear() {
13374af6e282f8a8f75928a071e8200039517cf5c12Chris Craik    for (auto& entry : mPool) {
1349fded232a9548a304e0145011df8849fba0dcda7Chris Craik        delete entry.layer;
1359fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
1369fded232a9548a304e0145011df8849fba0dcda7Chris Craik    mPool.clear();
1379fded232a9548a304e0145011df8849fba0dcda7Chris Craik    mSize = 0;
1389fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1399fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1409fded232a9548a304e0145011df8849fba0dcda7Chris CraikOffscreenBuffer* OffscreenBufferPool::get(RenderState& renderState,
1419fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const uint32_t width, const uint32_t height) {
1429fded232a9548a304e0145011df8849fba0dcda7Chris Craik    OffscreenBuffer* layer = nullptr;
1439fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1449fded232a9548a304e0145011df8849fba0dcda7Chris Craik    Entry entry(width, height);
1459fded232a9548a304e0145011df8849fba0dcda7Chris Craik    auto iter = mPool.find(entry);
1469fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1479fded232a9548a304e0145011df8849fba0dcda7Chris Craik    if (iter != mPool.end()) {
1489fded232a9548a304e0145011df8849fba0dcda7Chris Craik        entry = *iter;
1499fded232a9548a304e0145011df8849fba0dcda7Chris Craik        mPool.erase(iter);
1509fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1519fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer = entry.layer;
1529fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer->viewportWidth = width;
1539fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer->viewportHeight = height;
1549fded232a9548a304e0145011df8849fba0dcda7Chris Craik        mSize -= layer->getSizeInBytes();
1559fded232a9548a304e0145011df8849fba0dcda7Chris Craik    } else {
1569fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer = new OffscreenBuffer(renderState, Caches::getInstance(), width, height);
1579fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
1589fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1599fded232a9548a304e0145011df8849fba0dcda7Chris Craik    return layer;
1609fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1619fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1629fded232a9548a304e0145011df8849fba0dcda7Chris CraikOffscreenBuffer* OffscreenBufferPool::resize(OffscreenBuffer* layer,
1639fded232a9548a304e0145011df8849fba0dcda7Chris Craik        const uint32_t width, const uint32_t height) {
1649fded232a9548a304e0145011df8849fba0dcda7Chris Craik    RenderState& renderState = layer->renderState;
16538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    if (layer->texture.width() == OffscreenBuffer::computeIdealDimension(width)
16638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            && layer->texture.height() == OffscreenBuffer::computeIdealDimension(height)) {
1679fded232a9548a304e0145011df8849fba0dcda7Chris Craik        // resize in place
1689fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer->viewportWidth = width;
1699fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer->viewportHeight = height;
17002806288d1c56475413888a934c796e6e4eb11c5Chris Craik
17102806288d1c56475413888a934c796e6e4eb11c5Chris Craik        // entire area will be repainted (and may be smaller) so clear usage region
17202806288d1c56475413888a934c796e6e4eb11c5Chris Craik        layer->region.clear();
1739fded232a9548a304e0145011df8849fba0dcda7Chris Craik        return layer;
1749fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
1759fded232a9548a304e0145011df8849fba0dcda7Chris Craik    putOrDelete(layer);
1769fded232a9548a304e0145011df8849fba0dcda7Chris Craik    return get(renderState, width, height);
1779fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1789fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1799fded232a9548a304e0145011df8849fba0dcda7Chris Craikvoid OffscreenBufferPool::dump() {
1809fded232a9548a304e0145011df8849fba0dcda7Chris Craik    for (auto entry : mPool) {
1819fded232a9548a304e0145011df8849fba0dcda7Chris Craik        ALOGD("  Layer size %dx%d", entry.width, entry.height);
1829fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
1839fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1849fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1859fded232a9548a304e0145011df8849fba0dcda7Chris Craikvoid OffscreenBufferPool::putOrDelete(OffscreenBuffer* layer) {
1869fded232a9548a304e0145011df8849fba0dcda7Chris Craik    const uint32_t size = layer->getSizeInBytes();
1879fded232a9548a304e0145011df8849fba0dcda7Chris Craik    // Don't even try to cache a layer that's bigger than the cache
1889fded232a9548a304e0145011df8849fba0dcda7Chris Craik    if (size < mMaxSize) {
1899fded232a9548a304e0145011df8849fba0dcda7Chris Craik        // TODO: Use an LRU
1909fded232a9548a304e0145011df8849fba0dcda7Chris Craik        while (mSize + size > mMaxSize) {
1919fded232a9548a304e0145011df8849fba0dcda7Chris Craik            OffscreenBuffer* victim = mPool.begin()->layer;
1929fded232a9548a304e0145011df8849fba0dcda7Chris Craik            mSize -= victim->getSizeInBytes();
1939fded232a9548a304e0145011df8849fba0dcda7Chris Craik            delete victim;
1949fded232a9548a304e0145011df8849fba0dcda7Chris Craik            mPool.erase(mPool.begin());
1959fded232a9548a304e0145011df8849fba0dcda7Chris Craik        }
1969fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1979fded232a9548a304e0145011df8849fba0dcda7Chris Craik        // clear region, since it's no longer valid
1989fded232a9548a304e0145011df8849fba0dcda7Chris Craik        layer->region.clear();
1999fded232a9548a304e0145011df8849fba0dcda7Chris Craik
2009fded232a9548a304e0145011df8849fba0dcda7Chris Craik        Entry entry(layer);
2019fded232a9548a304e0145011df8849fba0dcda7Chris Craik
2029fded232a9548a304e0145011df8849fba0dcda7Chris Craik        mPool.insert(entry);
2039fded232a9548a304e0145011df8849fba0dcda7Chris Craik        mSize += size;
2049fded232a9548a304e0145011df8849fba0dcda7Chris Craik    } else {
2059fded232a9548a304e0145011df8849fba0dcda7Chris Craik        delete layer;
2069fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
2079fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
2089fded232a9548a304e0145011df8849fba0dcda7Chris Craik
2099fded232a9548a304e0145011df8849fba0dcda7Chris Craik}; // namespace uirenderer
2109fded232a9548a304e0145011df8849fba0dcda7Chris Craik}; // namespace android
211