RenderBufferCache.h revision bef837dc57b47fd7fcc17c86d741cf77eac4487b
1/* 2 * Copyright (C) 2013 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_RENDER_BUFFER_CACHE_H 18#define ANDROID_HWUI_RENDER_BUFFER_CACHE_H 19 20#include <GLES2/gl2.h> 21 22#include "RenderBuffer.h" 23 24#include <set> 25 26namespace android { 27namespace uirenderer { 28 29class RenderBufferCache { 30public: 31 RenderBufferCache(); 32 ~RenderBufferCache(); 33 34 /** 35 * Returns a buffer with the exact specified dimensions. If no suitable 36 * buffer can be found, a new one is created and returned. If creating a 37 * new buffer fails, NULL is returned. 38 * 39 * When a buffer is obtained from the cache, it is removed and the total 40 * size of the cache goes down. 41 * 42 * The returned buffer is always allocated and bound 43 * (see RenderBuffer::isAllocated()). 44 * 45 * @param format The desired render buffer format 46 * @param width The desired width of the buffer 47 * @param height The desired height of the buffer 48 */ 49 RenderBuffer* get(GLenum format, const uint32_t width, const uint32_t height); 50 51 /** 52 * Adds the buffer to the cache. The buffer will not be added if there is 53 * not enough space available. Adding a buffer can cause other buffer to 54 * be removed from the cache. 55 * 56 * @param buffer The render buffer to add to the cache 57 * 58 * @return True if the buffer was added, false otherwise. 59 */ 60 bool put(RenderBuffer* buffer); 61 /** 62 * Clears the cache. This causes all layers to be deleted. 63 */ 64 void clear(); 65 66 /** 67 * Sets the maximum size of the cache in bytes. 68 */ 69 void setMaxSize(uint32_t maxSize); 70 /** 71 * Returns the maximum size of the cache in bytes. 72 */ 73 uint32_t getMaxSize(); 74 /** 75 * Returns the current size of the cache in bytes. 76 */ 77 uint32_t getSize(); 78 79private: 80 struct RenderBufferEntry { 81 RenderBufferEntry(): 82 mBuffer(nullptr), mWidth(0), mHeight(0) { 83 } 84 85 RenderBufferEntry(GLenum format, const uint32_t width, const uint32_t height): 86 mBuffer(nullptr), mFormat(format), mWidth(width), mHeight(height) { 87 } 88 89 RenderBufferEntry(RenderBuffer* buffer): 90 mBuffer(buffer), mFormat(buffer->getFormat()), 91 mWidth(buffer->getWidth()), mHeight(buffer->getHeight()) { 92 } 93 94 static int compare(const RenderBufferEntry& lhs, const RenderBufferEntry& rhs); 95 96 bool operator==(const RenderBufferEntry& other) const { 97 return compare(*this, other) == 0; 98 } 99 100 bool operator!=(const RenderBufferEntry& other) const { 101 return compare(*this, other) != 0; 102 } 103 104 bool operator<(const RenderBufferEntry& other) const { 105 return RenderBufferEntry::compare(*this, other) < 0; 106 } 107 108 RenderBuffer* mBuffer; 109 GLenum mFormat; 110 uint32_t mWidth; 111 uint32_t mHeight; 112 }; // struct RenderBufferEntry 113 114 void deleteBuffer(RenderBuffer* buffer); 115 116 std::multiset<RenderBufferEntry> mCache; 117 118 uint32_t mSize; 119 uint32_t mMaxSize; 120}; // class RenderBufferCache 121 122}; // namespace uirenderer 123}; // namespace android 124 125#endif // ANDROID_HWUI_RENDER_BUFFER_CACHE_H 126