2010-08-04 15:40:07 -07:00
|
|
|
/*
|
2013-03-15 19:06:39 -07:00
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
2010-08-04 15:40:07 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-03-15 19:06:39 -07:00
|
|
|
#include <SkBitmap.h>
|
|
|
|
#include <SkCanvas.h>
|
2014-07-17 12:25:11 -07:00
|
|
|
#include <SkColor.h>
|
2013-03-15 19:06:39 -07:00
|
|
|
#include <SkPaint.h>
|
|
|
|
#include <SkPath.h>
|
|
|
|
#include <SkRect.h>
|
2010-09-08 18:04:33 -07:00
|
|
|
|
2013-03-15 19:06:39 -07:00
|
|
|
#include <utils/JenkinsHash.h>
|
|
|
|
#include <utils/Trace.h>
|
2013-03-08 17:44:20 -08:00
|
|
|
|
|
|
|
#include "Caches.h"
|
2010-08-04 15:40:07 -07:00
|
|
|
#include "PathCache.h"
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
#include "thread/Signal.h"
|
|
|
|
#include "thread/TaskProcessor.h"
|
2010-08-04 15:40:07 -07:00
|
|
|
|
2015-11-03 10:09:59 -08:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2010-08-04 15:40:07 -07:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2013-03-15 19:06:39 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cache entries
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-13 15:07:52 -07:00
|
|
|
PathDescription::PathDescription()
|
|
|
|
: type(kShapeNone)
|
|
|
|
, join(SkPaint::kDefault_Join)
|
|
|
|
, cap(SkPaint::kDefault_Cap)
|
|
|
|
, style(SkPaint::kFill_Style)
|
|
|
|
, miter(4.0f)
|
|
|
|
, strokeWidth(1.0f)
|
|
|
|
, pathEffect(nullptr) {
|
2013-03-15 19:06:39 -07:00
|
|
|
memset(&shape, 0, sizeof(Shape));
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:07:52 -07:00
|
|
|
PathDescription::PathDescription(ShapeType type, const SkPaint* paint)
|
|
|
|
: type(type)
|
|
|
|
, join(paint->getStrokeJoin())
|
|
|
|
, cap(paint->getStrokeCap())
|
|
|
|
, style(paint->getStyle())
|
|
|
|
, miter(paint->getStrokeMiter())
|
|
|
|
, strokeWidth(paint->getStrokeWidth())
|
|
|
|
, pathEffect(paint->getPathEffect()) {
|
2013-03-15 19:06:39 -07:00
|
|
|
memset(&shape, 0, sizeof(Shape));
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_t PathDescription::hash() const {
|
|
|
|
uint32_t hash = JenkinsHashMix(0, type);
|
|
|
|
hash = JenkinsHashMix(hash, join);
|
|
|
|
hash = JenkinsHashMix(hash, cap);
|
|
|
|
hash = JenkinsHashMix(hash, style);
|
|
|
|
hash = JenkinsHashMix(hash, android::hash_type(miter));
|
|
|
|
hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
|
|
|
|
hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
|
|
|
|
hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
|
|
|
|
return JenkinsHashWhiten(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utilities
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
// NOTE: This should only be used after PathTessellator handles joins properly
|
2015-01-05 15:51:13 -08:00
|
|
|
return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity;
|
2013-03-15 19:06:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint,
|
|
|
|
float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
|
|
|
|
const SkRect& bounds = path->getBounds();
|
|
|
|
PathCache::computeBounds(bounds, paint, left, top, offset, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint,
|
|
|
|
float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
|
2015-07-07 18:42:17 -07:00
|
|
|
const float pathWidth = std::max(bounds.width(), 1.0f);
|
|
|
|
const float pathHeight = std::max(bounds.height(), 1.0f);
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
left = bounds.fLeft;
|
|
|
|
top = bounds.fTop;
|
|
|
|
|
2015-07-07 18:42:17 -07:00
|
|
|
offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
width = uint32_t(pathWidth + offset * 2.0 + 0.5);
|
|
|
|
height = uint32_t(pathHeight + offset * 2.0 + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) {
|
2014-06-16 17:31:48 -04:00
|
|
|
bitmap.allocPixels(SkImageInfo::MakeA8(width, height));
|
2013-03-15 19:06:39 -07:00
|
|
|
bitmap.eraseColor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initPaint(SkPaint& paint) {
|
|
|
|
// Make sure the paint is opaque, color, alpha, filter, etc.
|
|
|
|
// will be applied later when compositing the alpha8 texture
|
2014-07-17 12:25:11 -07:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
2013-03-15 19:06:39 -07:00
|
|
|
paint.setAlpha(255);
|
2015-01-05 15:51:13 -08:00
|
|
|
paint.setColorFilter(nullptr);
|
|
|
|
paint.setMaskFilter(nullptr);
|
|
|
|
paint.setShader(nullptr);
|
2013-03-15 19:06:39 -07:00
|
|
|
SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
|
|
|
|
SkSafeUnref(paint.setXfermode(mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap,
|
|
|
|
float left, float top, float offset, uint32_t width, uint32_t height) {
|
|
|
|
initBitmap(bitmap, width, height);
|
|
|
|
|
|
|
|
SkPaint pathPaint(*paint);
|
|
|
|
initPaint(pathPaint);
|
|
|
|
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
canvas.translate(-left + offset, -top + offset);
|
|
|
|
canvas.drawPath(*path, pathPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cache constructor/destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-02-05 15:59:29 -08:00
|
|
|
PathCache::PathCache()
|
|
|
|
: mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity)
|
|
|
|
, mSize(0)
|
|
|
|
, mMaxSize(Properties::pathCacheSize) {
|
2013-03-15 19:06:39 -07:00
|
|
|
mCache.setOnEntryRemovedListener(this);
|
|
|
|
|
|
|
|
GLint maxTextureSize;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
|
|
|
|
mMaxTextureSize = maxTextureSize;
|
|
|
|
|
2015-05-04 14:36:49 -07:00
|
|
|
mDebugEnabled = Properties::debugLevel & kDebugCaches;
|
2013-03-15 19:06:39 -07:00
|
|
|
}
|
|
|
|
|
2014-06-02 16:27:04 -07:00
|
|
|
PathCache::~PathCache() {
|
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:06:39 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Size management
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
uint32_t PathCache::getSize() {
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PathCache::getMaxSize() {
|
|
|
|
return mMaxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Callbacks
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-11-22 00:35:09 +00:00
|
|
|
void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
|
2013-03-15 19:06:39 -07:00
|
|
|
removeTexture(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Caching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void PathCache::removeTexture(PathTexture* texture) {
|
|
|
|
if (texture) {
|
2015-11-10 12:19:17 -08:00
|
|
|
const uint32_t size = texture->width() * texture->height();
|
2013-08-21 18:40:24 -07:00
|
|
|
|
|
|
|
// If there is a pending task we must wait for it to return
|
|
|
|
// before attempting our cleanup
|
|
|
|
const sp<Task<SkBitmap*> >& task = texture->task();
|
2015-01-05 15:51:13 -08:00
|
|
|
if (task != nullptr) {
|
2014-11-10 15:23:43 -08:00
|
|
|
task->getResult();
|
2013-08-21 18:40:24 -07:00
|
|
|
texture->clearTask();
|
|
|
|
} else {
|
|
|
|
// If there is a pending task, the path was not added
|
|
|
|
// to the cache and the size wasn't increased
|
|
|
|
if (size > mSize) {
|
|
|
|
ALOGE("Removing path texture of size %d will leave "
|
|
|
|
"the cache in an inconsistent state", size);
|
|
|
|
}
|
|
|
|
mSize -= size;
|
|
|
|
}
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
|
|
|
|
texture->id, size, mSize);
|
|
|
|
if (mDebugEnabled) {
|
|
|
|
ALOGD("Shape deleted, size = %d", size);
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:19:17 -08:00
|
|
|
texture->deleteTexture();
|
2013-03-15 19:06:39 -07:00
|
|
|
delete texture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::purgeCache(uint32_t width, uint32_t height) {
|
|
|
|
const uint32_t size = width * height;
|
|
|
|
// Don't even try to cache a bitmap that's bigger than the cache
|
|
|
|
if (size < mMaxSize) {
|
|
|
|
while (mSize + size > mMaxSize) {
|
|
|
|
mCache.removeOldest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::trim() {
|
|
|
|
while (mSize > mMaxSize) {
|
|
|
|
mCache.removeOldest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
|
|
|
|
const SkPaint* paint) {
|
2014-11-18 10:49:23 -08:00
|
|
|
ATRACE_NAME("Generate Path Texture");
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
float left, top, offset;
|
|
|
|
uint32_t width, height;
|
|
|
|
computePathBounds(path, paint, left, top, offset, width, height);
|
|
|
|
|
2015-01-05 15:51:13 -08:00
|
|
|
if (!checkTextureSize(width, height)) return nullptr;
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
purgeCache(width, height);
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
drawPath(path, paint, bitmap, left, top, offset, width, height);
|
|
|
|
|
2015-03-13 15:07:52 -07:00
|
|
|
PathTexture* texture = new PathTexture(Caches::getInstance(),
|
2015-11-10 12:19:17 -08:00
|
|
|
left, top, offset, path->getGenerationID());
|
2013-03-26 17:29:51 -07:00
|
|
|
generateTexture(entry, &bitmap, texture);
|
2013-03-15 19:06:39 -07:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2013-03-26 17:29:51 -07:00
|
|
|
void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
|
|
|
|
PathTexture* texture, bool addToCache) {
|
2013-03-15 19:06:39 -07:00
|
|
|
generateTexture(*bitmap, texture);
|
|
|
|
|
2015-05-11 18:23:09 -07:00
|
|
|
// Note here that we upload to a texture even if it's bigger than mMaxSize.
|
|
|
|
// Such an entry in mCache will only be temporary, since it will be evicted
|
|
|
|
// immediately on trim, or on any other Path entering the cache.
|
2015-11-10 12:19:17 -08:00
|
|
|
uint32_t size = texture->width() * texture->height();
|
2015-05-11 18:23:09 -07:00
|
|
|
mSize += size;
|
|
|
|
PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
|
|
|
|
texture->id, size, mSize);
|
|
|
|
if (mDebugEnabled) {
|
|
|
|
ALOGD("Shape created, size = %d", size);
|
|
|
|
}
|
|
|
|
if (addToCache) {
|
|
|
|
mCache.put(entry, texture);
|
2013-03-15 19:06:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::clear() {
|
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
|
2015-05-13 17:05:48 -07:00
|
|
|
ATRACE_NAME("Upload Path Texture");
|
2015-11-10 12:19:17 -08:00
|
|
|
texture->upload(bitmap);
|
2013-03-15 19:06:39 -07:00
|
|
|
texture->setFilter(GL_LINEAR);
|
|
|
|
}
|
|
|
|
|
2013-03-08 17:44:20 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Path precaching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-03-11 20:48:31 -07:00
|
|
|
PathCache::PathProcessor::PathProcessor(Caches& caches):
|
|
|
|
TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
|
2012-04-27 11:47:13 -07:00
|
|
|
}
|
2012-02-16 19:24:51 -08:00
|
|
|
|
2013-03-11 20:48:31 -07:00
|
|
|
void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
|
2014-06-02 16:27:04 -07:00
|
|
|
PathTask* t = static_cast<PathTask*>(task.get());
|
2013-03-11 20:48:31 -07:00
|
|
|
ATRACE_NAME("pathPrecache");
|
|
|
|
|
|
|
|
float left, top, offset;
|
|
|
|
uint32_t width, height;
|
2014-06-27 18:30:23 -07:00
|
|
|
PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
|
2013-03-11 20:48:31 -07:00
|
|
|
|
|
|
|
PathTexture* texture = t->texture;
|
|
|
|
texture->left = left;
|
|
|
|
texture->top = top;
|
|
|
|
texture->offset = offset;
|
|
|
|
|
|
|
|
if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
|
|
|
|
SkBitmap* bitmap = new SkBitmap();
|
2014-06-27 18:30:23 -07:00
|
|
|
drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
|
2013-03-11 20:48:31 -07:00
|
|
|
t->setResult(bitmap);
|
|
|
|
} else {
|
2015-01-05 15:51:13 -08:00
|
|
|
t->setResult(nullptr);
|
2013-03-08 17:44:20 -08:00
|
|
|
}
|
2012-02-16 19:24:51 -08:00
|
|
|
}
|
|
|
|
|
2010-08-04 15:40:07 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-03-15 19:06:39 -07:00
|
|
|
// Paths
|
2010-08-04 15:40:07 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-02-12 14:10:21 -05:00
|
|
|
void PathCache::removeDeferred(const SkPath* path) {
|
2013-03-08 17:44:20 -08:00
|
|
|
Mutex::Autolock l(mLock);
|
2015-07-29 16:48:58 -07:00
|
|
|
mGarbage.push_back(path->getGenerationID());
|
2010-11-11 15:36:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathCache::clearGarbage() {
|
2013-06-26 15:45:41 -07:00
|
|
|
Vector<PathDescription> pathsToRemove;
|
|
|
|
|
|
|
|
{ // scope for the mutex
|
|
|
|
Mutex::Autolock l(mLock);
|
2015-07-29 16:48:58 -07:00
|
|
|
for (const uint32_t generationID : mGarbage) {
|
2015-02-12 14:10:21 -05:00
|
|
|
LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
|
|
|
|
while (iter.next()) {
|
|
|
|
const PathDescription& key = iter.key();
|
|
|
|
if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
|
|
|
|
pathsToRemove.push(key);
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 15:45:41 -07:00
|
|
|
}
|
|
|
|
mGarbage.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < pathsToRemove.size(); i++) {
|
|
|
|
mCache.remove(pathsToRemove.itemAt(i));
|
2010-11-11 15:36:56 -08:00
|
|
|
}
|
2010-09-08 18:04:33 -07:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapePath, paint);
|
2015-02-12 14:10:21 -05:00
|
|
|
entry.shape.path.mGenerationID = path->getGenerationID();
|
2013-03-15 19:06:39 -07:00
|
|
|
|
2010-08-04 15:40:07 -07:00
|
|
|
PathTexture* texture = mCache.get(entry);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
texture = addTexture(entry, path, paint);
|
2013-03-08 17:44:20 -08:00
|
|
|
} else {
|
|
|
|
// A bitmap is attached to the texture, this means we need to
|
|
|
|
// upload it as a GL texture
|
2013-03-11 20:48:31 -07:00
|
|
|
const sp<Task<SkBitmap*> >& task = texture->task();
|
2015-01-05 15:51:13 -08:00
|
|
|
if (task != nullptr) {
|
2013-03-08 17:44:20 -08:00
|
|
|
// But we must first wait for the worker thread to be done
|
|
|
|
// producing the bitmap, so let's wait
|
2013-03-11 20:48:31 -07:00
|
|
|
SkBitmap* bitmap = task->getResult();
|
2013-03-08 17:44:20 -08:00
|
|
|
if (bitmap) {
|
2013-03-26 17:29:51 -07:00
|
|
|
generateTexture(entry, bitmap, texture, false);
|
2013-03-11 20:48:31 -07:00
|
|
|
texture->clearTask();
|
2013-03-08 17:44:20 -08:00
|
|
|
} else {
|
2013-03-13 14:31:46 -07:00
|
|
|
ALOGW("Path too large to be rendered into a texture");
|
2013-03-11 20:48:31 -07:00
|
|
|
texture->clearTask();
|
2015-01-05 15:51:13 -08:00
|
|
|
texture = nullptr;
|
2013-03-08 17:44:20 -08:00
|
|
|
mCache.remove(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:00:28 +05:30
|
|
|
void PathCache::remove(const SkPath* path, const SkPaint* paint)
|
|
|
|
{
|
|
|
|
PathDescription entry(kShapePath, paint);
|
|
|
|
entry.shape.path.mGenerationID = path->getGenerationID();
|
|
|
|
mCache.remove(entry);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
void PathCache::precache(const SkPath* path, const SkPaint* paint) {
|
2013-03-11 20:48:31 -07:00
|
|
|
if (!Caches::getInstance().tasks.canRunTasks()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapePath, paint);
|
2015-02-12 14:10:21 -05:00
|
|
|
entry.shape.path.mGenerationID = path->getGenerationID();
|
2013-03-15 19:06:39 -07:00
|
|
|
|
2013-03-08 17:44:20 -08:00
|
|
|
PathTexture* texture = mCache.get(entry);
|
|
|
|
|
|
|
|
bool generate = false;
|
|
|
|
if (!texture) {
|
|
|
|
generate = true;
|
2010-08-04 15:40:07 -07:00
|
|
|
}
|
|
|
|
|
2013-03-08 17:44:20 -08:00
|
|
|
if (generate) {
|
|
|
|
// It is important to specify the generation ID so we do not
|
|
|
|
// attempt to precache the same path several times
|
2015-03-13 15:07:52 -07:00
|
|
|
texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
|
2013-03-11 20:48:31 -07:00
|
|
|
sp<PathTask> task = new PathTask(path, paint, texture);
|
|
|
|
texture->setTask(task);
|
2013-03-08 17:44:20 -08:00
|
|
|
|
|
|
|
// During the precaching phase we insert path texture objects into
|
|
|
|
// the cache that do not point to any GL texture. They are instead
|
|
|
|
// treated as a task for the precaching worker thread. This is why
|
|
|
|
// we do not check the cache limit when inserting these objects.
|
|
|
|
// The conversion into GL texture will happen in get(), when a client
|
|
|
|
// asks for a path texture. This is also when the cache limit will
|
|
|
|
// be enforced.
|
|
|
|
mCache.put(entry, texture);
|
2013-03-11 20:48:31 -07:00
|
|
|
|
2015-01-05 15:51:13 -08:00
|
|
|
if (mProcessor == nullptr) {
|
2013-03-11 20:48:31 -07:00
|
|
|
mProcessor = new PathProcessor(Caches::getInstance());
|
|
|
|
}
|
2015-04-20 14:54:49 -07:00
|
|
|
mProcessor->add(task);
|
2013-03-08 17:44:20 -08:00
|
|
|
}
|
2010-08-04 15:40:07 -07:00
|
|
|
}
|
|
|
|
|
2013-03-15 19:06:39 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Rounded rects
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
PathTexture* PathCache::getRoundRect(float width, float height,
|
2014-01-02 17:13:34 -08:00
|
|
|
float rx, float ry, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapeRoundRect, paint);
|
|
|
|
entry.shape.roundRect.mWidth = width;
|
|
|
|
entry.shape.roundRect.mHeight = height;
|
|
|
|
entry.shape.roundRect.mRx = rx;
|
|
|
|
entry.shape.roundRect.mRy = ry;
|
|
|
|
|
|
|
|
PathTexture* texture = get(entry);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
SkPath path;
|
|
|
|
SkRect r;
|
|
|
|
r.set(0.0f, 0.0f, width, height);
|
|
|
|
path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
|
|
|
|
|
|
|
|
texture = addTexture(entry, &path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Circles
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapeCircle, paint);
|
|
|
|
entry.shape.circle.mRadius = radius;
|
|
|
|
|
|
|
|
PathTexture* texture = get(entry);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
SkPath path;
|
|
|
|
path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
|
|
|
|
|
|
|
|
texture = addTexture(entry, &path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Ovals
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapeOval, paint);
|
|
|
|
entry.shape.oval.mWidth = width;
|
|
|
|
entry.shape.oval.mHeight = height;
|
|
|
|
|
|
|
|
PathTexture* texture = get(entry);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
SkPath path;
|
|
|
|
SkRect r;
|
|
|
|
r.set(0.0f, 0.0f, width, height);
|
|
|
|
path.addOval(r, SkPath::kCW_Direction);
|
|
|
|
|
|
|
|
texture = addTexture(entry, &path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Rects
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapeRect, paint);
|
|
|
|
entry.shape.rect.mWidth = width;
|
|
|
|
entry.shape.rect.mHeight = height;
|
|
|
|
|
|
|
|
PathTexture* texture = get(entry);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
SkPath path;
|
|
|
|
SkRect r;
|
|
|
|
r.set(0.0f, 0.0f, width, height);
|
|
|
|
path.addRect(r, SkPath::kCW_Direction);
|
|
|
|
|
|
|
|
texture = addTexture(entry, &path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Arcs
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
PathTexture* PathCache::getArc(float width, float height,
|
2014-01-02 17:13:34 -08:00
|
|
|
float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
|
2013-03-15 19:06:39 -07:00
|
|
|
PathDescription entry(kShapeArc, paint);
|
|
|
|
entry.shape.arc.mWidth = width;
|
|
|
|
entry.shape.arc.mHeight = height;
|
|
|
|
entry.shape.arc.mStartAngle = startAngle;
|
|
|
|
entry.shape.arc.mSweepAngle = sweepAngle;
|
|
|
|
entry.shape.arc.mUseCenter = useCenter;
|
|
|
|
|
|
|
|
PathTexture* texture = get(entry);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
SkPath path;
|
|
|
|
SkRect r;
|
|
|
|
r.set(0.0f, 0.0f, width, height);
|
|
|
|
if (useCenter) {
|
|
|
|
path.moveTo(r.centerX(), r.centerY());
|
|
|
|
}
|
|
|
|
path.arcTo(r, startAngle, sweepAngle, !useCenter);
|
|
|
|
if (useCenter) {
|
|
|
|
path.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = addTexture(entry, &path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2010-08-04 15:40:07 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|