1 /* 2 * Copyright (C) 2015 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 "OffscreenBufferPool.h" 18 19 #include "Caches.h" 20 #include "Properties.h" 21 #include "renderstate/RenderState.h" 22 #include "utils/FatVector.h" 23 #include "utils/TraceUtils.h" 24 25 #include <utils/Color.h> 26 #include <utils/Log.h> 27 28 #include <GLES2/gl2.h> 29 30 namespace android { 31 namespace uirenderer { 32 33 //////////////////////////////////////////////////////////////////////////////// 34 // OffscreenBuffer 35 //////////////////////////////////////////////////////////////////////////////// 36 37 OffscreenBuffer::OffscreenBuffer(RenderState& renderState, Caches& caches, 38 uint32_t viewportWidth, uint32_t viewportHeight) 39 : GpuMemoryTracker(GpuObjectType::OffscreenBuffer) 40 , renderState(renderState) 41 , viewportWidth(viewportWidth) 42 , viewportHeight(viewportHeight) 43 , texture(caches) { 44 uint32_t width = computeIdealDimension(viewportWidth); 45 uint32_t height = computeIdealDimension(viewportHeight); 46 ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height); 47 caches.textureState().activateTexture(0); 48 texture.resize(width, height, caches.rgbaInternalFormat(), GL_RGBA); 49 texture.blend = true; 50 texture.setWrap(GL_CLAMP_TO_EDGE); 51 // not setting filter on texture, since it's set when drawing, based on transform 52 } 53 54 Rect OffscreenBuffer::getTextureCoordinates() { 55 const float texX = 1.0f / static_cast<float>(texture.width()); 56 const float texY = 1.0f / static_cast<float>(texture.height()); 57 return Rect(0, viewportHeight * texY, viewportWidth * texX, 0); 58 } 59 60 void OffscreenBuffer::dirty(Rect dirtyArea) { 61 dirtyArea.doIntersect(0, 0, viewportWidth, viewportHeight); 62 if (!dirtyArea.isEmpty()) { 63 region.orSelf(android::Rect(dirtyArea.left, dirtyArea.top, 64 dirtyArea.right, dirtyArea.bottom)); 65 } 66 } 67 68 void OffscreenBuffer::updateMeshFromRegion() { 69 // avoid T-junctions as they cause artifacts in between the resultant 70 // geometry when complex transforms occur. 71 // TODO: generate the safeRegion only if necessary based on drawing transform 72 Region safeRegion = Region::createTJunctionFreeRegion(region); 73 74 size_t count; 75 const android::Rect* rects = safeRegion.getArray(&count); 76 77 const float texX = 1.0f / float(texture.width()); 78 const float texY = 1.0f / float(texture.height()); 79 80 FatVector<TextureVertex, 64> meshVector(count * 4); // uses heap if more than 64 vertices needed 81 TextureVertex* mesh = &meshVector[0]; 82 for (size_t i = 0; i < count; i++) { 83 const android::Rect* r = &rects[i]; 84 85 const float u1 = r->left * texX; 86 const float v1 = (viewportHeight - r->top) * texY; 87 const float u2 = r->right * texX; 88 const float v2 = (viewportHeight - r->bottom) * texY; 89 90 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 91 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 92 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 93 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 94 } 95 elementCount = count * 6; 96 renderState.meshState().genOrUpdateMeshBuffer(&vbo, 97 sizeof(TextureVertex) * count * 4, 98 &meshVector[0], 99 GL_DYNAMIC_DRAW); // TODO: GL_STATIC_DRAW if savelayer 100 } 101 102 uint32_t OffscreenBuffer::computeIdealDimension(uint32_t dimension) { 103 return uint32_t(ceilf(dimension / float(LAYER_SIZE)) * LAYER_SIZE); 104 } 105 106 OffscreenBuffer::~OffscreenBuffer() { 107 ATRACE_FORMAT("Destroy %ux%u HW Layer", texture.width(), texture.height()); 108 texture.deleteTexture(); 109 renderState.meshState().deleteMeshBuffer(vbo); 110 elementCount = 0; 111 vbo = 0; 112 } 113 114 /////////////////////////////////////////////////////////////////////////////// 115 // OffscreenBufferPool 116 /////////////////////////////////////////////////////////////////////////////// 117 118 OffscreenBufferPool::OffscreenBufferPool() 119 : mMaxSize(Properties::layerPoolSize) { 120 } 121 122 OffscreenBufferPool::~OffscreenBufferPool() { 123 clear(); // TODO: unique_ptr? 124 } 125 126 int OffscreenBufferPool::Entry::compare(const Entry& lhs, const Entry& rhs) { 127 int deltaInt = int(lhs.width) - int(rhs.width); 128 if (deltaInt != 0) return deltaInt; 129 130 return int(lhs.height) - int(rhs.height); 131 } 132 133 void OffscreenBufferPool::clear() { 134 for (auto& entry : mPool) { 135 delete entry.layer; 136 } 137 mPool.clear(); 138 mSize = 0; 139 } 140 141 OffscreenBuffer* OffscreenBufferPool::get(RenderState& renderState, 142 const uint32_t width, const uint32_t height) { 143 OffscreenBuffer* layer = nullptr; 144 145 Entry entry(width, height); 146 auto iter = mPool.find(entry); 147 148 if (iter != mPool.end()) { 149 entry = *iter; 150 mPool.erase(iter); 151 152 layer = entry.layer; 153 layer->viewportWidth = width; 154 layer->viewportHeight = height; 155 mSize -= layer->getSizeInBytes(); 156 } else { 157 layer = new OffscreenBuffer(renderState, Caches::getInstance(), width, height); 158 } 159 160 return layer; 161 } 162 163 OffscreenBuffer* OffscreenBufferPool::resize(OffscreenBuffer* layer, 164 const uint32_t width, const uint32_t height) { 165 RenderState& renderState = layer->renderState; 166 if (layer->texture.width() == OffscreenBuffer::computeIdealDimension(width) 167 && layer->texture.height() == OffscreenBuffer::computeIdealDimension(height)) { 168 // resize in place 169 layer->viewportWidth = width; 170 layer->viewportHeight = height; 171 172 // entire area will be repainted (and may be smaller) so clear usage region 173 layer->region.clear(); 174 return layer; 175 } 176 putOrDelete(layer); 177 return get(renderState, width, height); 178 } 179 180 void OffscreenBufferPool::dump() { 181 for (auto entry : mPool) { 182 ALOGD(" Layer size %dx%d", entry.width, entry.height); 183 } 184 } 185 186 void OffscreenBufferPool::putOrDelete(OffscreenBuffer* layer) { 187 const uint32_t size = layer->getSizeInBytes(); 188 // Don't even try to cache a layer that's bigger than the cache 189 if (size < mMaxSize) { 190 // TODO: Use an LRU 191 while (mSize + size > mMaxSize) { 192 OffscreenBuffer* victim = mPool.begin()->layer; 193 mSize -= victim->getSizeInBytes(); 194 delete victim; 195 mPool.erase(mPool.begin()); 196 } 197 198 // clear region, since it's no longer valid 199 layer->region.clear(); 200 201 Entry entry(layer); 202 203 mPool.insert(entry); 204 mSize += size; 205 } else { 206 delete layer; 207 } 208 } 209 210 }; // namespace uirenderer 211 }; // namespace android 212