GradientCache.cpp revision fe48f65922d4a3cc4aefe058cee5acec51504a20
1/* 2 * Copyright (C) 2010 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#define LOG_TAG "OpenGLRenderer" 18 19#include <GLES2/gl2.h> 20 21#include <SkCanvas.h> 22#include <SkGradientShader.h> 23 24#include <utils/threads.h> 25 26#include "GradientCache.h" 27#include "Properties.h" 28 29namespace android { 30namespace uirenderer { 31 32/////////////////////////////////////////////////////////////////////////////// 33// Constructors/destructor 34/////////////////////////////////////////////////////////////////////////////// 35 36GradientCache::GradientCache(): 37 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity), 38 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) { 39 char property[PROPERTY_VALUE_MAX]; 40 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { 41 LOGD(" Setting gradient cache size to %sMB", property); 42 setMaxSize(MB(atof(property))); 43 } else { 44 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); 45 } 46 47 mCache.setOnEntryRemovedListener(this); 48} 49 50GradientCache::GradientCache(uint32_t maxByteSize): 51 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity), 52 mSize(0), mMaxSize(maxByteSize) { 53 mCache.setOnEntryRemovedListener(this); 54} 55 56GradientCache::~GradientCache() { 57 mCache.clear(); 58} 59 60/////////////////////////////////////////////////////////////////////////////// 61// Size management 62/////////////////////////////////////////////////////////////////////////////// 63 64uint32_t GradientCache::getSize() { 65 return mSize; 66} 67 68uint32_t GradientCache::getMaxSize() { 69 return mMaxSize; 70} 71 72void GradientCache::setMaxSize(uint32_t maxSize) { 73 mMaxSize = maxSize; 74 while (mSize > mMaxSize) { 75 mCache.removeOldest(); 76 } 77} 78 79/////////////////////////////////////////////////////////////////////////////// 80// Callbacks 81/////////////////////////////////////////////////////////////////////////////// 82 83void GradientCache::operator()(SkShader*& shader, Texture*& texture) { 84 // Already locked here 85 if (shader) { 86 const uint32_t size = texture->width * texture->height * 4; 87 mSize -= size; 88 } 89 90 if (texture) { 91 glDeleteTextures(1, &texture->id); 92 delete texture; 93 } 94} 95 96/////////////////////////////////////////////////////////////////////////////// 97// Caching 98/////////////////////////////////////////////////////////////////////////////// 99 100Texture* GradientCache::get(SkShader* shader) { 101 return mCache.get(shader); 102} 103 104void GradientCache::remove(SkShader* shader) { 105 mCache.remove(shader); 106} 107 108void GradientCache::removeDeferred(SkShader* shader) { 109 Mutex::Autolock _l(mLock); 110 mGarbage.push(shader); 111} 112 113void GradientCache::clearGarbage() { 114 Mutex::Autolock _l(mLock); 115 size_t count = mGarbage.size(); 116 for (size_t i = 0; i < count; i++) { 117 mCache.remove(mGarbage.itemAt(i)); 118 } 119 mGarbage.clear(); 120} 121 122void GradientCache::clear() { 123 mCache.clear(); 124} 125 126Texture* GradientCache::addLinearGradient(SkShader* shader, uint32_t* colors, 127 float* positions, int count, SkShader::TileMode tileMode) { 128 SkBitmap bitmap; 129 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1); 130 bitmap.allocPixels(); 131 bitmap.eraseColor(0); 132 133 SkCanvas canvas(bitmap); 134 135 SkPoint points[2]; 136 points[0].set(0.0f, 0.0f); 137 points[1].set(bitmap.width(), 0.0f); 138 139 SkShader* localShader = SkGradientShader::CreateLinear(points, 140 reinterpret_cast<const SkColor*>(colors), positions, count, tileMode); 141 142 SkPaint p; 143 p.setStyle(SkPaint::kStrokeAndFill_Style); 144 p.setShader(localShader)->unref(); 145 146 canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p); 147 148 // Asume the cache is always big enough 149 const uint32_t size = bitmap.rowBytes() * bitmap.height(); 150 while (mSize + size > mMaxSize) { 151 mCache.removeOldest(); 152 } 153 154 Texture* texture = new Texture; 155 generateTexture(&bitmap, texture); 156 157 mSize += size; 158 mCache.put(shader, texture); 159 160 return texture; 161} 162 163void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) { 164 SkAutoLockPixels autoLock(*bitmap); 165 if (!bitmap->readyToDraw()) { 166 LOGE("Cannot generate texture from shader"); 167 return; 168 } 169 170 texture->generation = bitmap->getGenerationID(); 171 texture->width = bitmap->width(); 172 texture->height = bitmap->height(); 173 174 glGenTextures(1, &texture->id); 175 176 glBindTexture(GL_TEXTURE_2D, texture->id); 177 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 178 179 texture->blend = !bitmap->isOpaque(); 180 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, 181 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); 182 183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 185 186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 188} 189 190}; // namespace uirenderer 191}; // namespace android 192