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 #ifndef ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H 18 #define ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H 19 20 #include <GpuMemoryTracker.h> 21 #include <ui/Region.h> 22 #include "Caches.h" 23 #include "Texture.h" 24 #include "utils/Macros.h" 25 26 #include <set> 27 28 namespace android { 29 namespace uirenderer { 30 31 class RenderState; 32 33 /** 34 * Lightweight alternative to Layer. Owns the persistent state of an offscreen render target, and 35 * encompasses enough information to draw it back on screen (minus paint properties, which are held 36 * by LayerOp). 37 * 38 * Has two distinct sizes - viewportWidth/viewportHeight describe content area, 39 * texture.width/.height are actual allocated texture size. Texture will tend to be larger than the 40 * viewport bounds, since textures are always allocated with width / height as a multiple of 64, for 41 * the purpose of improving reuse. 42 */ 43 class OffscreenBuffer : GpuMemoryTracker { 44 public: 45 OffscreenBuffer(RenderState& renderState, Caches& caches, uint32_t viewportWidth, 46 uint32_t viewportHeight, bool wideColorGamut = false); 47 ~OffscreenBuffer(); 48 49 Rect getTextureCoordinates(); 50 51 void dirty(Rect dirtyArea); 52 53 // must be called prior to rendering, to construct/update vertex buffer 54 void updateMeshFromRegion(); 55 56 // Set by RenderNode for HW layers, TODO for clipped saveLayers setWindowTransform(const Matrix4 & transform)57 void setWindowTransform(const Matrix4& transform) { 58 inverseTransformInWindow.loadInverse(transform); 59 } 60 61 static uint32_t computeIdealDimension(uint32_t dimension); 62 getSizeInBytes()63 uint32_t getSizeInBytes() { return texture.objectSize(); } 64 65 RenderState& renderState; 66 67 uint32_t viewportWidth; 68 uint32_t viewportHeight; 69 Texture texture; 70 71 bool wideColorGamut = false; 72 73 // Portion of layer that has been drawn to. Used to minimize drawing area when 74 // drawing back to screen / parent FBO. 75 Region region; 76 77 Matrix4 inverseTransformInWindow; 78 79 // vbo / size of mesh 80 GLsizei elementCount = 0; 81 GLuint vbo = 0; 82 83 bool hasRenderedSinceRepaint; 84 }; 85 86 /** 87 * Pool of OffscreenBuffers allocated, but not currently in use. 88 */ 89 class OffscreenBufferPool { 90 public: 91 OffscreenBufferPool(); 92 ~OffscreenBufferPool(); 93 94 WARN_UNUSED_RESULT OffscreenBuffer* get(RenderState& renderState, const uint32_t width, 95 const uint32_t height, bool wideColorGamut = false); 96 97 WARN_UNUSED_RESULT OffscreenBuffer* resize(OffscreenBuffer* layer, const uint32_t width, 98 const uint32_t height); 99 100 void putOrDelete(OffscreenBuffer* layer); 101 102 /** 103 * Clears the pool. This causes all layers to be deleted. 104 */ 105 void clear(); 106 107 /** 108 * Returns the maximum size of the pool in bytes. 109 */ getMaxSize()110 uint32_t getMaxSize() { return mMaxSize; } 111 112 /** 113 * Returns the current size of the pool in bytes. 114 */ getSize()115 uint32_t getSize() { return mSize; } 116 getCount()117 size_t getCount() { return mPool.size(); } 118 119 /** 120 * Prints out the content of the pool. 121 */ 122 void dump(); 123 124 private: 125 struct Entry { EntryEntry126 Entry() {} 127 EntryEntry128 Entry(const uint32_t layerWidth, const uint32_t layerHeight, bool wideColorGamut) 129 : width(OffscreenBuffer::computeIdealDimension(layerWidth)) 130 , height(OffscreenBuffer::computeIdealDimension(layerHeight)) 131 , wideColorGamut(wideColorGamut) {} 132 EntryEntry133 explicit Entry(OffscreenBuffer* layer) 134 : layer(layer) 135 , width(layer->texture.width()) 136 , height(layer->texture.height()) 137 , wideColorGamut(layer->wideColorGamut) {} 138 139 static int compare(const Entry& lhs, const Entry& rhs); 140 141 bool operator==(const Entry& other) const { return compare(*this, other) == 0; } 142 143 bool operator!=(const Entry& other) const { return compare(*this, other) != 0; } 144 145 bool operator<(const Entry& other) const { return Entry::compare(*this, other) < 0; } 146 147 OffscreenBuffer* layer = nullptr; 148 uint32_t width = 0; 149 uint32_t height = 0; 150 bool wideColorGamut = false; 151 }; // struct Entry 152 153 std::multiset<Entry> mPool; 154 155 uint32_t mSize = 0; 156 uint32_t mMaxSize; 157 }; // class OffscreenBufferCache 158 159 }; // namespace uirenderer 160 }; // namespace android 161 162 #endif // ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H 163