blob: fb687cd16ff531be02e63b82638888604412aab8 [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -07001/*
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 Guyca89e2a2013-03-08 17:44:20 -080019#include <utils/Mutex.h>
Romain Guya2341a92010-09-08 18:04:33 -070020
Romain Guyca89e2a2013-03-08 17:44:20 -080021#include <sys/sysinfo.h>
22
23#include "Caches.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070024#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070025#include "Properties.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070026
27namespace android {
28namespace uirenderer {
29
Romain Guyca89e2a2013-03-08 17:44:20 -080030///////////////////////////////////////////////////////////////////////////////
31// Path precaching
32///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -070033
Romain Guy5dc7fa72013-03-11 20:48:31 -070034PathCache::PathProcessor::PathProcessor(Caches& caches):
35 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -070036}
Romain Guy33f6beb2012-02-16 19:24:51 -080037
Romain Guy5dc7fa72013-03-11 20:48:31 -070038void 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 Guy0f809f32013-03-13 14:31:46 -070058 texture->width = 0;
59 texture->height = 0;
Romain Guy5dc7fa72013-03-11 20:48:31 -070060 t->setResult(NULL);
Romain Guyca89e2a2013-03-08 17:44:20 -080061 }
Romain Guy33f6beb2012-02-16 19:24:51 -080062}
63
Romain Guy7fbcc042010-08-04 15:40:07 -070064///////////////////////////////////////////////////////////////////////////////
Romain Guyff26a0c2011-01-20 11:35:46 -080065// Path cache
Romain Guy7fbcc042010-08-04 15:40:07 -070066///////////////////////////////////////////////////////////////////////////////
67
Romain Guyff26a0c2011-01-20 11:35:46 -080068PathCache::PathCache(): ShapeCache<PathCacheEntry>("path",
Romain Guy5dc7fa72013-03-11 20:48:31 -070069 PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) {
Romain Guyca89e2a2013-03-08 17:44:20 -080070}
71
72PathCache::~PathCache() {
Romain Guy7fbcc042010-08-04 15:40:07 -070073}
74
Romain Guya2341a92010-09-08 18:04:33 -070075void PathCache::remove(SkPath* path) {
Romain Guy059e12c2012-11-28 17:35:51 -080076 Vector<PathCacheEntry> pathsToRemove;
77 LruCache<PathCacheEntry, PathTexture*>::Iterator i(mCache);
78
79 while (i.next()) {
80 const PathCacheEntry& key = i.key();
Romain Guyca89e2a2013-03-08 17:44:20 -080081 if (key.path == path || key.path == path->getSourcePath()) {
Romain Guy059e12c2012-11-28 17:35:51 -080082 pathsToRemove.push(key);
Romain Guya2341a92010-09-08 18:04:33 -070083 }
84 }
Romain Guy9e108412010-11-09 14:35:20 -080085
86 for (size_t i = 0; i < pathsToRemove.size(); i++) {
Romain Guy059e12c2012-11-28 17:35:51 -080087 mCache.remove(pathsToRemove.itemAt(i));
Romain Guy9e108412010-11-09 14:35:20 -080088 }
Romain Guyfe48f652010-11-11 15:36:56 -080089}
90
91void PathCache::removeDeferred(SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -080092 Mutex::Autolock l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -080093 mGarbage.push(path);
94}
95
96void PathCache::clearGarbage() {
Romain Guyca89e2a2013-03-08 17:44:20 -080097 Mutex::Autolock l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -080098 size_t count = mGarbage.size();
99 for (size_t i = 0; i < count; i++) {
100 remove(mGarbage.itemAt(i));
101 }
102 mGarbage.clear();
Romain Guya2341a92010-09-08 18:04:33 -0700103}
104
Romain Guyca89e2a2013-03-08 17:44:20 -0800105/**
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 */
113static SkPath* getSourcePath(SkPath* path) {
Romain Guy4bcb7462012-02-23 17:08:38 -0800114 const SkPath* sourcePath = path->getSourcePath();
115 if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800116 return const_cast<SkPath*>(sourcePath);
Romain Guy4bcb7462012-02-23 17:08:38 -0800117 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800118 return path;
119}
120
121PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
122 path = getSourcePath(path);
Romain Guy4bcb7462012-02-23 17:08:38 -0800123
Romain Guy7fbcc042010-08-04 15:40:07 -0700124 PathCacheEntry entry(path, paint);
125 PathTexture* texture = mCache.get(entry);
126
127 if (!texture) {
128 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800129 } else {
130 // A bitmap is attached to the texture, this means we need to
131 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700132 const sp<Task<SkBitmap*> >& task = texture->task();
133 if (task != NULL) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800134 // But we must first wait for the worker thread to be done
135 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700136 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800137 if (bitmap) {
138 addTexture(entry, bitmap, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700139 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800140 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700141 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700142 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800143 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 Guy7fbcc042010-08-04 15:40:07 -0700150 }
151
Romain Guy7fbcc042010-08-04 15:40:07 -0700152 return texture;
153}
154
Romain Guyca89e2a2013-03-08 17:44:20 -0800155void PathCache::precache(SkPath* path, SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700156 if (!Caches::getInstance().tasks.canRunTasks()) {
157 return;
158 }
159
Romain Guyca89e2a2013-03-08 17:44:20 -0800160 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 Guy5dc7fa72013-03-11 20:48:31 -0700176 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 Guyca89e2a2013-03-08 17:44:20 -0800179
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 Guy5dc7fa72013-03-11 20:48:31 -0700188
189 if (mProcessor == NULL) {
190 mProcessor = new PathProcessor(Caches::getInstance());
191 }
192 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800193 }
194}
195
Romain Guy7fbcc042010-08-04 15:40:07 -0700196}; // namespace uirenderer
197}; // namespace android