blob: eca5e4d201f58767c6933a64569892db310bae0e [file] [log] [blame]
Romain Guyf7f93552010-07-08 19:17:03 -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
19#include <utils/Log.h>
20#include <utils/ResourceTypes.h>
21
22#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070023#include "Properties.h"
Romain Guyf7f93552010-07-08 19:17:03 -070024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Constructors/destructor
30///////////////////////////////////////////////////////////////////////////////
31
Romain Guy2728f962010-10-08 18:36:15 -070032PatchCache::PatchCache(): mMaxEntries(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guyfb8b7632010-08-23 21:05:08 -070033}
34
Romain Guy2728f962010-10-08 18:36:15 -070035PatchCache::PatchCache(uint32_t maxEntries): mMaxEntries(maxEntries) {
Romain Guyf7f93552010-07-08 19:17:03 -070036}
37
38PatchCache::~PatchCache() {
39 clear();
40}
41
42///////////////////////////////////////////////////////////////////////////////
Romain Guyf7f93552010-07-08 19:17:03 -070043// Caching
44///////////////////////////////////////////////////////////////////////////////
45
46void PatchCache::clear() {
Romain Guy2728f962010-10-08 18:36:15 -070047 size_t count = mCache.size();
48 for (int i = 0; i < count; i++) {
49 delete mCache.valueAt(i);
50 }
Romain Guyf7f93552010-07-08 19:17:03 -070051 mCache.clear();
Romain Guyf7f93552010-07-08 19:17:03 -070052}
53
Romain Guy2728f962010-10-08 18:36:15 -070054Patch* PatchCache::get(const float bitmapWidth, const float bitmapHeight,
55 const float pixelWidth, const float pixelHeight,
56 const int32_t* xDivs, const int32_t* yDivs,
57 const uint32_t width, const uint32_t height) {
Romain Guyf7f93552010-07-08 19:17:03 -070058
Romain Guy2728f962010-10-08 18:36:15 -070059 const PatchDescription description(bitmapWidth, bitmapHeight,
60 pixelWidth, pixelHeight, width, height);
61
62 ssize_t index = mCache.indexOfKey(description);
63 Patch* mesh = NULL;
64 if (index >= 0) {
65 mesh = mCache.valueAt(index);
66 }
67
Romain Guyf7f93552010-07-08 19:17:03 -070068 if (!mesh) {
69 PATCH_LOGD("Creating new patch mesh, w=%d h=%d", width, height);
Romain Guy2728f962010-10-08 18:36:15 -070070
Romain Guyf7f93552010-07-08 19:17:03 -070071 mesh = new Patch(width, height);
Romain Guy2728f962010-10-08 18:36:15 -070072 mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f,
73 pixelWidth, pixelHeight, xDivs, yDivs, width, height);
74
75 if (mCache.size() >= mMaxEntries) {
Romain Guy21b028a2010-10-08 18:43:58 -070076 delete mCache.valueAt(mCache.size() - 1);
77 mCache.removeItemsAt(mCache.size() - 1, 1);
Romain Guy2728f962010-10-08 18:36:15 -070078 }
79
80 mCache.add(description, mesh);
Romain Guyf7f93552010-07-08 19:17:03 -070081 }
82
83 return mesh;
84}
85
86}; // namespace uirenderer
87}; // namespace android