Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 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 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 24 | #include <utils/threads.h> |
| 25 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 26 | #include "Debug.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 27 | #include "GradientCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 28 | #include "Properties.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | // Constructors/destructor |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 37 | GradientCache::GradientCache(): |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 38 | mCache(GenerationCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 39 | mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) { |
| 40 | char property[PROPERTY_VALUE_MAX]; |
| 41 | if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 42 | INIT_LOGD(" Setting gradient cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 43 | setMaxSize(MB(atof(property))); |
| 44 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 45 | INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | mCache.setOnEntryRemovedListener(this); |
| 49 | } |
| 50 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 51 | GradientCache::GradientCache(uint32_t maxByteSize): |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 52 | mCache(GenerationCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity), |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 53 | mSize(0), mMaxSize(maxByteSize) { |
| 54 | mCache.setOnEntryRemovedListener(this); |
| 55 | } |
| 56 | |
| 57 | GradientCache::~GradientCache() { |
| 58 | mCache.clear(); |
| 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 | // Size management |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | uint32_t GradientCache::getSize() { |
| 66 | return mSize; |
| 67 | } |
| 68 | |
| 69 | uint32_t GradientCache::getMaxSize() { |
| 70 | return mMaxSize; |
| 71 | } |
| 72 | |
| 73 | void GradientCache::setMaxSize(uint32_t maxSize) { |
| 74 | mMaxSize = maxSize; |
| 75 | while (mSize > mMaxSize) { |
| 76 | mCache.removeOldest(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | // Callbacks |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 84 | void GradientCache::operator()(GradientCacheEntry& shader, Texture*& texture) { |
| 85 | if (texture) { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 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 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 100 | Texture* GradientCache::get(uint32_t* colors, float* positions, |
| 101 | int count, SkShader::TileMode tileMode) { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 102 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 103 | GradientCacheEntry gradient(colors, positions, count, tileMode); |
| 104 | Texture* texture = mCache.get(gradient); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 105 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 106 | if (!texture) { |
| 107 | texture = addLinearGradient(gradient, colors, positions, count, tileMode); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 108 | } |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 109 | |
| 110 | return texture; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void GradientCache::clear() { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 114 | mCache.clear(); |
| 115 | } |
| 116 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 117 | Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient, |
| 118 | uint32_t* colors, float* positions, int count, SkShader::TileMode tileMode) { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 119 | SkBitmap bitmap; |
| 120 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1); |
| 121 | bitmap.allocPixels(); |
| 122 | bitmap.eraseColor(0); |
| 123 | |
| 124 | SkCanvas canvas(bitmap); |
| 125 | |
| 126 | SkPoint points[2]; |
| 127 | points[0].set(0.0f, 0.0f); |
| 128 | points[1].set(bitmap.width(), 0.0f); |
| 129 | |
| 130 | SkShader* localShader = SkGradientShader::CreateLinear(points, |
| 131 | reinterpret_cast<const SkColor*>(colors), positions, count, tileMode); |
| 132 | |
| 133 | SkPaint p; |
| 134 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 135 | p.setShader(localShader)->unref(); |
| 136 | |
| 137 | canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p); |
| 138 | |
| 139 | // Asume the cache is always big enough |
| 140 | const uint32_t size = bitmap.rowBytes() * bitmap.height(); |
| 141 | while (mSize + size > mMaxSize) { |
| 142 | mCache.removeOldest(); |
| 143 | } |
| 144 | |
| 145 | Texture* texture = new Texture; |
| 146 | generateTexture(&bitmap, texture); |
| 147 | |
| 148 | mSize += size; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 149 | mCache.put(gradient, texture); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 150 | |
| 151 | return texture; |
| 152 | } |
| 153 | |
| 154 | void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) { |
| 155 | SkAutoLockPixels autoLock(*bitmap); |
| 156 | if (!bitmap->readyToDraw()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 157 | ALOGE("Cannot generate texture from shader"); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | texture->generation = bitmap->getGenerationID(); |
| 162 | texture->width = bitmap->width(); |
| 163 | texture->height = bitmap->height(); |
| 164 | |
| 165 | glGenTextures(1, &texture->id); |
| 166 | |
| 167 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 168 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
| 169 | |
| 170 | texture->blend = !bitmap->isOpaque(); |
| 171 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 172 | GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 173 | |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 174 | texture->setFilter(GL_LINEAR); |
| 175 | texture->setWrap(GL_CLAMP_TO_EDGE); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | }; // namespace uirenderer |
| 179 | }; // namespace android |