Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -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 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 19 | #include <utils/Mutex.h> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 20 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 21 | #include <sys/sysinfo.h> |
| 22 | |
| 23 | #include "Caches.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 24 | #include "PathCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 25 | #include "Properties.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // Path precaching |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 33 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 34 | PathCache::PathProcessor::PathProcessor(Caches& caches): |
| 35 | TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) { |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 36 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 37 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 38 | void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) { |
| 39 | sp<PathTask> t = static_cast<PathTask* >(task.get()); |
| 40 | ATRACE_NAME("pathPrecache"); |
| 41 | |
| 42 | float left, top, offset; |
| 43 | uint32_t width, height; |
| 44 | PathCache::computePathBounds(t->path, t->paint, left, top, offset, width, height); |
| 45 | |
| 46 | PathTexture* texture = t->texture; |
| 47 | texture->left = left; |
| 48 | texture->top = top; |
| 49 | texture->offset = offset; |
| 50 | texture->width = width; |
| 51 | texture->height = height; |
| 52 | |
| 53 | if (width <= mMaxTextureSize && height <= mMaxTextureSize) { |
| 54 | SkBitmap* bitmap = new SkBitmap(); |
| 55 | PathCache::drawPath(t->path, t->paint, *bitmap, left, top, offset, width, height); |
| 56 | t->setResult(bitmap); |
| 57 | } else { |
Romain Guy | 0f809f3 | 2013-03-13 14:31:46 -0700 | [diff] [blame] | 58 | texture->width = 0; |
| 59 | texture->height = 0; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 60 | t->setResult(NULL); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 61 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 64 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 65 | // Path cache |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 68 | PathCache::PathCache(): ShapeCache<PathCacheEntry>("path", |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 69 | PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | PathCache::~PathCache() { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 75 | void PathCache::remove(SkPath* path) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 76 | Vector<PathCacheEntry> pathsToRemove; |
| 77 | LruCache<PathCacheEntry, PathTexture*>::Iterator i(mCache); |
| 78 | |
| 79 | while (i.next()) { |
| 80 | const PathCacheEntry& key = i.key(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 81 | if (key.path == path || key.path == path->getSourcePath()) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 82 | pathsToRemove.push(key); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 85 | |
| 86 | for (size_t i = 0; i < pathsToRemove.size(); i++) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 87 | mCache.remove(pathsToRemove.itemAt(i)); |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 88 | } |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void PathCache::removeDeferred(SkPath* path) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 92 | Mutex::Autolock l(mLock); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 93 | mGarbage.push(path); |
| 94 | } |
| 95 | |
| 96 | void PathCache::clearGarbage() { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 97 | Mutex::Autolock l(mLock); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 98 | size_t count = mGarbage.size(); |
| 99 | for (size_t i = 0; i < count; i++) { |
| 100 | remove(mGarbage.itemAt(i)); |
| 101 | } |
| 102 | mGarbage.clear(); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 105 | /** |
| 106 | * To properly handle path mutations at draw time we always make a copy |
| 107 | * of paths objects when recording display lists. The source path points |
| 108 | * to the path we originally copied the path from. This ensures we use |
| 109 | * the original path as a cache key the first time a path is inserted |
| 110 | * in the cache. The source path is also used to reclaim garbage when a |
| 111 | * Dalvik Path object is collected. |
| 112 | */ |
| 113 | static SkPath* getSourcePath(SkPath* path) { |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 114 | const SkPath* sourcePath = path->getSourcePath(); |
| 115 | if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 116 | return const_cast<SkPath*>(sourcePath); |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 117 | } |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 118 | return path; |
| 119 | } |
| 120 | |
| 121 | PathTexture* PathCache::get(SkPath* path, SkPaint* paint) { |
| 122 | path = getSourcePath(path); |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 123 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 124 | PathCacheEntry entry(path, paint); |
| 125 | PathTexture* texture = mCache.get(entry); |
| 126 | |
| 127 | if (!texture) { |
| 128 | texture = addTexture(entry, path, paint); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 129 | } else { |
| 130 | // A bitmap is attached to the texture, this means we need to |
| 131 | // upload it as a GL texture |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 132 | const sp<Task<SkBitmap*> >& task = texture->task(); |
| 133 | if (task != NULL) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 134 | // But we must first wait for the worker thread to be done |
| 135 | // producing the bitmap, so let's wait |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 136 | SkBitmap* bitmap = task->getResult(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 137 | if (bitmap) { |
| 138 | addTexture(entry, bitmap, texture); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 139 | texture->clearTask(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 140 | } else { |
Romain Guy | 0f809f3 | 2013-03-13 14:31:46 -0700 | [diff] [blame] | 141 | ALOGW("Path too large to be rendered into a texture"); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 142 | texture->clearTask(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 143 | texture = NULL; |
| 144 | mCache.remove(entry); |
| 145 | } |
| 146 | } else if (path->getGenerationID() != texture->generation) { |
| 147 | mCache.remove(entry); |
| 148 | texture = addTexture(entry, path, paint); |
| 149 | } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 152 | return texture; |
| 153 | } |
| 154 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 155 | void PathCache::precache(SkPath* path, SkPaint* paint) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 156 | if (!Caches::getInstance().tasks.canRunTasks()) { |
| 157 | return; |
| 158 | } |
| 159 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 160 | path = getSourcePath(path); |
| 161 | |
| 162 | PathCacheEntry entry(path, paint); |
| 163 | PathTexture* texture = mCache.get(entry); |
| 164 | |
| 165 | bool generate = false; |
| 166 | if (!texture) { |
| 167 | generate = true; |
| 168 | } else if (path->getGenerationID() != texture->generation) { |
| 169 | mCache.remove(entry); |
| 170 | generate = true; |
| 171 | } |
| 172 | |
| 173 | if (generate) { |
| 174 | // It is important to specify the generation ID so we do not |
| 175 | // attempt to precache the same path several times |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 176 | texture = createTexture(0.0f, 0.0f, 0.0f, 0, 0, path->getGenerationID()); |
| 177 | sp<PathTask> task = new PathTask(path, paint, texture); |
| 178 | texture->setTask(task); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 179 | |
| 180 | // During the precaching phase we insert path texture objects into |
| 181 | // the cache that do not point to any GL texture. They are instead |
| 182 | // treated as a task for the precaching worker thread. This is why |
| 183 | // we do not check the cache limit when inserting these objects. |
| 184 | // The conversion into GL texture will happen in get(), when a client |
| 185 | // asks for a path texture. This is also when the cache limit will |
| 186 | // be enforced. |
| 187 | mCache.put(entry, texture); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 188 | |
| 189 | if (mProcessor == NULL) { |
| 190 | mProcessor = new PathProcessor(Caches::getInstance()); |
| 191 | } |
| 192 | mProcessor->add(task); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 196 | }; // namespace uirenderer |
| 197 | }; // namespace android |