PathCache.cpp revision ee248599d49a15fc207c5aeb0b90ec263cc1d600
17fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy/*
2c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy * Copyright (C) 2013 The Android Open Source Project
37fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy *
47fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
57fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * you may not use this file except in compliance with the License.
67fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * You may obtain a copy of the License at
77fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy *
87fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
97fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy *
107fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * Unless required by applicable law or agreed to in writing, software
117fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
127fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * See the License for the specific language governing permissions and
147fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy * limitations under the License.
157fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy */
167fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
177fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy#define LOG_TAG "OpenGLRenderer"
18c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#define ATRACE_TAG ATRACE_TAG_VIEW
197fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
20c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <SkBitmap.h>
21c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <SkCanvas.h>
2298d608dba6a0b3c15fb08f1fa2c8b9d170124c7cChris Craik#include <SkColor.h>
23c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <SkPaint.h>
24c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <SkPath.h>
25c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <SkRect.h>
26a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy
27c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <utils/JenkinsHash.h>
28c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include <utils/Trace.h>
29ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
30ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy#include "Caches.h"
317fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy#include "PathCache.h"
32c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
33c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include "thread/Signal.h"
34c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy#include "thread/TaskProcessor.h"
357fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
367fbcc0492fca03857e3c45064f4aa040af817d55Romain Guynamespace android {
377fbcc0492fca03857e3c45064f4aa040af817d55Romain Guynamespace uirenderer {
387fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
39ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy///////////////////////////////////////////////////////////////////////////////
40c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Cache entries
41c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
42c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
43c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain GuyPathDescription::PathDescription():
44c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        type(kShapeNone),
45c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        join(SkPaint::kDefault_Join),
46c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        cap(SkPaint::kDefault_Cap),
47c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        style(SkPaint::kFill_Style),
48c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        miter(4.0f),
49c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        strokeWidth(1.0f),
50d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        pathEffect(nullptr) {
51c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    memset(&shape, 0, sizeof(Shape));
52c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
53c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
54d218a92c0afb8c0d98135b20b52ac87236e1c935Chris CraikPathDescription::PathDescription(ShapeType type, const SkPaint* paint):
55c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        type(type),
56c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        join(paint->getStrokeJoin()),
57c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        cap(paint->getStrokeCap()),
58c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        style(paint->getStyle()),
59c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        miter(paint->getStrokeMiter()),
60c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        strokeWidth(paint->getStrokeWidth()),
61c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        pathEffect(paint->getPathEffect()) {
62c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    memset(&shape, 0, sizeof(Shape));
63c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
64c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
65c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyhash_t PathDescription::hash() const {
66c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    uint32_t hash = JenkinsHashMix(0, type);
67c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMix(hash, join);
68c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMix(hash, cap);
69c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMix(hash, style);
70c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMix(hash, android::hash_type(miter));
71c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
72c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
73c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
74c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return JenkinsHashWhiten(hash);
75c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
76c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
77c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
78c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Utilities
79c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
80c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
81d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craikbool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) {
82c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    // NOTE: This should only be used after PathTessellator handles joins properly
83d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity;
84c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
85c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
86c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::computePathBounds(const SkPath* path, const SkPaint* paint,
87c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
88c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    const SkRect& bounds = path->getBounds();
89c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathCache::computeBounds(bounds, paint, left, top, offset, width, height);
90c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
91c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
92c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint,
93c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
94c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    const float pathWidth = fmax(bounds.width(), 1.0f);
95c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    const float pathHeight = fmax(bounds.height(), 1.0f);
96c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
97c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    left = bounds.fLeft;
98c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    top = bounds.fTop;
99c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
100c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    offset = (int) floorf(fmax(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
101c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
102c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    width = uint32_t(pathWidth + offset * 2.0 + 0.5);
103c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    height = uint32_t(pathHeight + offset * 2.0 + 0.5);
104c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
105c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
106c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guystatic void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) {
107b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    bitmap.allocPixels(SkImageInfo::MakeA8(width, height));
108c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    bitmap.eraseColor(0);
109c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
110c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
111c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guystatic void initPaint(SkPaint& paint) {
112c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    // Make sure the paint is opaque, color, alpha, filter, etc.
113c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    // will be applied later when compositing the alpha8 texture
11498d608dba6a0b3c15fb08f1fa2c8b9d170124c7cChris Craik    paint.setColor(SK_ColorBLACK);
115c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    paint.setAlpha(255);
116d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    paint.setColorFilter(nullptr);
117d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    paint.setMaskFilter(nullptr);
118d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    paint.setShader(nullptr);
119c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
120c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    SkSafeUnref(paint.setXfermode(mode));
121c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
122c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
123c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guystatic void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap,
124c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        float left, float top, float offset, uint32_t width, uint32_t height) {
125c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    initBitmap(bitmap, width, height);
126c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
127c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    SkPaint pathPaint(*paint);
128c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    initPaint(pathPaint);
129c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
130c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    SkCanvas canvas(bitmap);
131c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    canvas.translate(-left + offset, -top + offset);
132c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    canvas.drawPath(*path, pathPaint);
133c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
134c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
135c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guystatic PathTexture* createTexture(float left, float top, float offset,
136c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        uint32_t width, uint32_t height, uint32_t id) {
1378aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    PathTexture* texture = new PathTexture(Caches::getInstance());
138c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->left = left;
139c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->top = top;
140c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->offset = offset;
141c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->width = width;
142c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->height = height;
143c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->generation = id;
144c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
145c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
146c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
147c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
148c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Cache constructor/destructor
149c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
150c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
151c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain GuyPathCache::PathCache():
152c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity),
153c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
154c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    char property[PROPERTY_VALUE_MAX];
155d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) {
156c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        INIT_LOGD("  Setting %s cache size to %sMB", name, property);
157c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        setMaxSize(MB(atof(property)));
158c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    } else {
159c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        INIT_LOGD("  Using default %s cache size of %.2fMB", name, DEFAULT_PATH_CACHE_SIZE);
160c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
161c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
162c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    mCache.setOnEntryRemovedListener(this);
163c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
164c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    GLint maxTextureSize;
165c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
166c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    mMaxTextureSize = maxTextureSize;
167c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
168c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    mDebugEnabled = readDebugLevel() & kDebugCaches;
169c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
170c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
17105f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris CraikPathCache::~PathCache() {
17205f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik    mCache.clear();
17305f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik}
17405f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik
175c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
176c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Size management
177c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
178c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
179c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyuint32_t PathCache::getSize() {
180c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return mSize;
181c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
182c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
183c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyuint32_t PathCache::getMaxSize() {
184c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return mMaxSize;
185c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
186c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
187c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::setMaxSize(uint32_t maxSize) {
188c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    mMaxSize = maxSize;
189c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    while (mSize > mMaxSize) {
190c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        mCache.removeOldest();
191c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
192c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
193c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
194c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
195c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Callbacks
196c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
197c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
19864bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
199c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    removeTexture(texture);
200c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
201c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
202c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
203c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Caching
204c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
205c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
206c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::removeTexture(PathTexture* texture) {
207c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (texture) {
208c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        const uint32_t size = texture->width * texture->height;
2095d923200846ed59e813373bde789d97d4ccc40b5Romain Guy
2105d923200846ed59e813373bde789d97d4ccc40b5Romain Guy        // If there is a pending task we must wait for it to return
2115d923200846ed59e813373bde789d97d4ccc40b5Romain Guy        // before attempting our cleanup
2125d923200846ed59e813373bde789d97d4ccc40b5Romain Guy        const sp<Task<SkBitmap*> >& task = texture->task();
213d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        if (task != nullptr) {
2141e19674107e1aa2224c2b8c7d12bfa057efe80eaAndreas Gampe            task->getResult();
2155d923200846ed59e813373bde789d97d4ccc40b5Romain Guy            texture->clearTask();
2165d923200846ed59e813373bde789d97d4ccc40b5Romain Guy        } else {
2175d923200846ed59e813373bde789d97d4ccc40b5Romain Guy            // If there is a pending task, the path was not added
2185d923200846ed59e813373bde789d97d4ccc40b5Romain Guy            // to the cache and the size wasn't increased
2195d923200846ed59e813373bde789d97d4ccc40b5Romain Guy            if (size > mSize) {
2205d923200846ed59e813373bde789d97d4ccc40b5Romain Guy                ALOGE("Removing path texture of size %d will leave "
2215d923200846ed59e813373bde789d97d4ccc40b5Romain Guy                        "the cache in an inconsistent state", size);
2225d923200846ed59e813373bde789d97d4ccc40b5Romain Guy            }
2235d923200846ed59e813373bde789d97d4ccc40b5Romain Guy            mSize -= size;
2245d923200846ed59e813373bde789d97d4ccc40b5Romain Guy        }
225c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
226c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
227c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy                texture->id, size, mSize);
228c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        if (mDebugEnabled) {
229c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            ALOGD("Shape deleted, size = %d", size);
230c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        }
231c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
232c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        if (texture->id) {
23344eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik            Caches::getInstance().textureState().deleteTexture(texture->id);
234c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        }
235c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        delete texture;
236c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
237c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
238c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
239c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::purgeCache(uint32_t width, uint32_t height) {
240c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    const uint32_t size = width * height;
241c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    // Don't even try to cache a bitmap that's bigger than the cache
242c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (size < mMaxSize) {
243c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        while (mSize + size > mMaxSize) {
244c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            mCache.removeOldest();
245c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        }
246c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
247c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
248c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
249c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::trim() {
250c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    while (mSize > mMaxSize) {
251c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        mCache.removeOldest();
252c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
253c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
254c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
255c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain GuyPathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
256c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        const SkPaint* paint) {
25770850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik    ATRACE_NAME("Generate Path Texture");
258c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
259c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    float left, top, offset;
260c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    uint32_t width, height;
261c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    computePathBounds(path, paint, left, top, offset, width, height);
262c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
263d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (!checkTextureSize(width, height)) return nullptr;
264c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
265c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    purgeCache(width, height);
266c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
267c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    SkBitmap bitmap;
268c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    drawPath(path, paint, bitmap, left, top, offset, width, height);
269c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
270c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathTexture* texture = createTexture(left, top, offset, width, height,
271c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            path->getGenerationID());
2724500a8d5d7fbec9dba5e693212da160849e401ffRomain Guy    generateTexture(entry, &bitmap, texture);
273c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
274c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
275c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
276c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
2774500a8d5d7fbec9dba5e693212da160849e401ffRomain Guyvoid PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
2784500a8d5d7fbec9dba5e693212da160849e401ffRomain Guy        PathTexture* texture, bool addToCache) {
279c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    generateTexture(*bitmap, texture);
280c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
281c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    uint32_t size = texture->width * texture->height;
282c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (size < mMaxSize) {
283c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        mSize += size;
284c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
285c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy                texture->id, size, mSize);
286c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        if (mDebugEnabled) {
287c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            ALOGD("Shape created, size = %d", size);
288c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        }
2894500a8d5d7fbec9dba5e693212da160849e401ffRomain Guy        if (addToCache) {
2904500a8d5d7fbec9dba5e693212da160849e401ffRomain Guy            mCache.put(entry, texture);
2914500a8d5d7fbec9dba5e693212da160849e401ffRomain Guy        }
292c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    } else {
2930a8c51b1d0d66d6060afcec1eab33091d49332aeRomain Guy        // It's okay to add a texture that's bigger than the cache since
2940a8c51b1d0d66d6060afcec1eab33091d49332aeRomain Guy        // we'll trim the cache later when addToCache is set to false
2950a8c51b1d0d66d6060afcec1eab33091d49332aeRomain Guy        if (!addToCache) {
2960a8c51b1d0d66d6060afcec1eab33091d49332aeRomain Guy            mSize += size;
2970a8c51b1d0d66d6060afcec1eab33091d49332aeRomain Guy        }
298c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        texture->cleanup = true;
299c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
300c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
301c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
302c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::clear() {
303c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    mCache.clear();
304c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
305c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
306c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guyvoid PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
307c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    SkAutoLockPixels alp(bitmap);
308c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (!bitmap.readyToDraw()) {
309c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        ALOGE("Cannot generate texture from bitmap");
310c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        return;
311c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
312c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
313c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    glGenTextures(1, &texture->id);
314c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
31544eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    Caches::getInstance().textureState().bindTexture(texture->id);
316c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    // Textures are Alpha8
317c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
318c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
319c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->blend = true;
320c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
321c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
322c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
323c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->setFilter(GL_LINEAR);
324c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    texture->setWrap(GL_CLAMP_TO_EDGE);
325c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
326c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
327c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
328ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy// Path precaching
329ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy///////////////////////////////////////////////////////////////////////////////
330ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
3315dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain GuyPathCache::PathProcessor::PathProcessor(Caches& caches):
3325dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
333fdd6fc1beb5076a630c7066b8b1731995636c09fRomain Guy}
33433f6beb10f98e8ba96250e284876d607055d278dRomain Guy
3355dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guyvoid PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
33605f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik    PathTask* t = static_cast<PathTask*>(task.get());
3375dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    ATRACE_NAME("pathPrecache");
3385dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy
3395dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    float left, top, offset;
3405dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    uint32_t width, height;
341906d47fd2bcedb9674b5765d01bd9c758069074cChris Craik    PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
3425dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy
3435dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    PathTexture* texture = t->texture;
3445dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    texture->left = left;
3455dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    texture->top = top;
3465dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    texture->offset = offset;
3475dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    texture->width = width;
3485dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    texture->height = height;
3495dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy
3505dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
3515dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        SkBitmap* bitmap = new SkBitmap();
352906d47fd2bcedb9674b5765d01bd9c758069074cChris Craik        drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
3535dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        t->setResult(bitmap);
3545dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    } else {
3550f809f3b794174f044366bf421f8d0c72d9afc14Romain Guy        texture->width = 0;
3560f809f3b794174f044366bf421f8d0c72d9afc14Romain Guy        texture->height = 0;
357d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        t->setResult(nullptr);
358ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    }
35933f6beb10f98e8ba96250e284876d607055d278dRomain Guy}
36033f6beb10f98e8ba96250e284876d607055d278dRomain Guy
3617fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy///////////////////////////////////////////////////////////////////////////////
362c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Paths
3637fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy///////////////////////////////////////////////////////////////////////////////
3647fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
365ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenbergervoid PathCache::removeDeferred(const SkPath* path) {
366ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    Mutex::Autolock l(mLock);
367ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger    mGarbage.push(path->getGenerationID());
368fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy}
369fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
370fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guyvoid PathCache::clearGarbage() {
371e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    Vector<PathDescription> pathsToRemove;
372e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
373e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    { // scope for the mutex
374e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        Mutex::Autolock l(mLock);
375e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        size_t count = mGarbage.size();
376e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        for (size_t i = 0; i < count; i++) {
377ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger            const uint32_t generationID = mGarbage.itemAt(i);
378ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger
379ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger            LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
380ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger            while (iter.next()) {
381ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger                const PathDescription& key = iter.key();
382ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger                if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
383ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger                    pathsToRemove.push(key);
384ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger                }
385ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger            }
386e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        }
387e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        mGarbage.clear();
388e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    }
389e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
390e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    for (size_t i = 0; i < pathsToRemove.size(); i++) {
391e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        mCache.remove(pathsToRemove.itemAt(i));
392fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    }
393a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy}
394a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy
395d218a92c0afb8c0d98135b20b52ac87236e1c935Chris CraikPathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
396c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapePath, paint);
397ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger    entry.shape.path.mGenerationID = path->getGenerationID();
398c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
3997fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy    PathTexture* texture = mCache.get(entry);
4007fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
4017fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy    if (!texture) {
4027fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy        texture = addTexture(entry, path, paint);
403ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    } else {
404ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // A bitmap is attached to the texture, this means we need to
405ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // upload it as a GL texture
4065dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        const sp<Task<SkBitmap*> >& task = texture->task();
407d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        if (task != nullptr) {
408ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy            // But we must first wait for the worker thread to be done
409ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy            // producing the bitmap, so let's wait
4105dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy            SkBitmap* bitmap = task->getResult();
411ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy            if (bitmap) {
4124500a8d5d7fbec9dba5e693212da160849e401ffRomain Guy                generateTexture(entry, bitmap, texture, false);
4135dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy                texture->clearTask();
414ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy            } else {
4150f809f3b794174f044366bf421f8d0c72d9afc14Romain Guy                ALOGW("Path too large to be rendered into a texture");
4165dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy                texture->clearTask();
417d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik                texture = nullptr;
418ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy                mCache.remove(entry);
419ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy            }
420ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        }
421ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    }
422ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
423ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    return texture;
424ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy}
425ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
426d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craikvoid PathCache::precache(const SkPath* path, const SkPaint* paint) {
4275dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    if (!Caches::getInstance().tasks.canRunTasks()) {
4285dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        return;
4295dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy    }
4305dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy
431c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapePath, paint);
432ee248599d49a15fc207c5aeb0b90ec263cc1d600Derek Sollenberger    entry.shape.path.mGenerationID = path->getGenerationID();
433c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
434ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    PathTexture* texture = mCache.get(entry);
435ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
436ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    bool generate = false;
437ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    if (!texture) {
438ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        generate = true;
4397fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy    }
4407fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
441ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    if (generate) {
442ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // It is important to specify the generation ID so we do not
443ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // attempt to precache the same path several times
4445dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        texture = createTexture(0.0f, 0.0f, 0.0f, 0, 0, path->getGenerationID());
4455dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        sp<PathTask> task = new PathTask(path, paint, texture);
4465dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        texture->setTask(task);
447ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
448ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // During the precaching phase we insert path texture objects into
449ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // the cache that do not point to any GL texture. They are instead
450ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // treated as a task for the precaching worker thread. This is why
451ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // we do not check the cache limit when inserting these objects.
452ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // The conversion into GL texture will happen in get(), when a client
453ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // asks for a path texture. This is also when the cache limit will
454ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        // be enforced.
455ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        mCache.put(entry, texture);
4565dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy
457d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        if (mProcessor == nullptr) {
4585dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy            mProcessor = new PathProcessor(Caches::getInstance());
4595dc7fa709646799a5207a5d217f70aa02bf4a3aaRomain Guy        }
460c3c58e015fa30a0ad87d4af2b95b7071baa8ffe4Sangkyu Lee        if (!mProcessor->add(task)) {
461c3c58e015fa30a0ad87d4af2b95b7071baa8ffe4Sangkyu Lee            mProcessor->process(task);
462c3c58e015fa30a0ad87d4af2b95b7071baa8ffe4Sangkyu Lee        }
463ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    }
4647fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}
4657fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
466c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
467c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Rounded rects
468c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
469c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
470c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain GuyPathTexture* PathCache::getRoundRect(float width, float height,
471d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik        float rx, float ry, const SkPaint* paint) {
472c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapeRoundRect, paint);
473c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.roundRect.mWidth = width;
474c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.roundRect.mHeight = height;
475c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.roundRect.mRx = rx;
476c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.roundRect.mRy = ry;
477c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
478c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathTexture* texture = get(entry);
479c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
480c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (!texture) {
481c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkPath path;
482c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkRect r;
483c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        r.set(0.0f, 0.0f, width, height);
484c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
485c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
486c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        texture = addTexture(entry, &path, paint);
487c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
488c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
489c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
490c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
491c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
492c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
493c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Circles
494c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
495c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
496d218a92c0afb8c0d98135b20b52ac87236e1c935Chris CraikPathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
497c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapeCircle, paint);
498c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.circle.mRadius = radius;
499c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
500c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathTexture* texture = get(entry);
501c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
502c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (!texture) {
503c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkPath path;
504c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
505c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
506c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        texture = addTexture(entry, &path, paint);
507c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
508c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
509c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
510c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
511c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
512c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
513c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Ovals
514c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
515c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
516d218a92c0afb8c0d98135b20b52ac87236e1c935Chris CraikPathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
517c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapeOval, paint);
518c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.oval.mWidth = width;
519c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.oval.mHeight = height;
520c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
521c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathTexture* texture = get(entry);
522c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
523c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (!texture) {
524c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkPath path;
525c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkRect r;
526c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        r.set(0.0f, 0.0f, width, height);
527c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        path.addOval(r, SkPath::kCW_Direction);
528c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
529c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        texture = addTexture(entry, &path, paint);
530c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
531c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
532c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
533c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
534c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
535c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
536c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Rects
537c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
538c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
539d218a92c0afb8c0d98135b20b52ac87236e1c935Chris CraikPathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
540c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapeRect, paint);
541c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.rect.mWidth = width;
542c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.rect.mHeight = height;
543c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
544c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathTexture* texture = get(entry);
545c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
546c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (!texture) {
547c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkPath path;
548c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkRect r;
549c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        r.set(0.0f, 0.0f, width, height);
550c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        path.addRect(r, SkPath::kCW_Direction);
551c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
552c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        texture = addTexture(entry, &path, paint);
553c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
554c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
555c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
556c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
557c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
558c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
559c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy// Arcs
560c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy///////////////////////////////////////////////////////////////////////////////
561c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
562c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain GuyPathTexture* PathCache::getArc(float width, float height,
563d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik        float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
564c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathDescription entry(kShapeArc, paint);
565c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.arc.mWidth = width;
566c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.arc.mHeight = height;
567c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.arc.mStartAngle = startAngle;
568c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.arc.mSweepAngle = sweepAngle;
569c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    entry.shape.arc.mUseCenter = useCenter;
570c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
571c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    PathTexture* texture = get(entry);
572c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
573c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    if (!texture) {
574c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkPath path;
575c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        SkRect r;
576c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        r.set(0.0f, 0.0f, width, height);
577c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        if (useCenter) {
578c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            path.moveTo(r.centerX(), r.centerY());
579c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        }
580c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        path.arcTo(r, startAngle, sweepAngle, !useCenter);
581c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        if (useCenter) {
582c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy            path.close();
583c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        }
584c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
585c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy        texture = addTexture(entry, &path, paint);
586c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    }
587c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
588c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy    return texture;
589c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy}
590c46d07a29e94807e768f8b162ce9f77a88ba6f46Romain Guy
5917fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}; // namespace uirenderer
5927fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}; // namespace android
593