blob: 4d2fc0143f98022a009babed15f38da0e80d11ec [file] [log] [blame]
Romain Guy3b748a42013-04-17 18:54:38 -07001/*
2 * Copyright (C) 2013 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
Romain Guy3b748a42013-04-17 18:54:38 -070017#include "AssetAtlas.h"
18
19#include <GLES2/gl2ext.h>
20
Romain Guy3b748a42013-04-17 18:54:38 -070021namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Lifecycle
26///////////////////////////////////////////////////////////////////////////////
27
28void AssetAtlas::init(sp<GraphicBuffer> buffer, int* map, int count) {
Romain Guy877cfe02013-05-02 17:36:28 -070029 if (mImage) {
Romain Guy3b748a42013-04-17 18:54:38 -070030 return;
31 }
32
Romain Guy877cfe02013-05-02 17:36:28 -070033 mImage = new Image(buffer);
34 mTexture = mImage->getTexture();
Romain Guy3b748a42013-04-17 18:54:38 -070035
Romain Guy877cfe02013-05-02 17:36:28 -070036 if (mTexture) {
37 mWidth = buffer->getWidth();
38 mHeight = buffer->getHeight();
Romain Guy3b748a42013-04-17 18:54:38 -070039
Romain Guy877cfe02013-05-02 17:36:28 -070040 createEntries(map, count);
41 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070042 ALOGW("Could not create atlas image");
43
Romain Guy877cfe02013-05-02 17:36:28 -070044 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070045 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070046 }
Romain Guy3b748a42013-04-17 18:54:38 -070047}
48
49void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070050 if (mImage) {
51 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070052 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070053
54 for (size_t i = 0; i < mEntries.size(); i++) {
55 delete mEntries.valueAt(i);
56 }
57 mEntries.clear();
58
59 mWidth = mHeight = 0;
60 }
61}
62
63///////////////////////////////////////////////////////////////////////////////
64// Entries
65///////////////////////////////////////////////////////////////////////////////
66
67AssetAtlas::Entry* AssetAtlas::getEntry(SkBitmap* const bitmap) const {
68 ssize_t index = mEntries.indexOfKey(bitmap);
69 return index >= 0 ? mEntries.valueAt(index) : NULL;
70}
71
72Texture* AssetAtlas::getEntryTexture(SkBitmap* const bitmap) const {
73 ssize_t index = mEntries.indexOfKey(bitmap);
74 return index >= 0 ? &mEntries.valueAt(index)->texture : NULL;
75}
76
77/**
78 * TODO: This method does not take the rotation flag into account
79 */
80void AssetAtlas::createEntries(int* map, int count) {
81 for (int i = 0; i < count; ) {
82 SkBitmap* bitmap = (SkBitmap*) map[i++];
83 int x = map[i++];
84 int y = map[i++];
85 bool rotated = map[i++] > 0;
86
87 // Bitmaps should never be null, we're just extra paranoid
88 if (!bitmap) continue;
89
90 const UvMapper mapper(
91 x / (float) mWidth, (x + bitmap->width()) / (float) mWidth,
92 y / (float) mHeight, (y + bitmap->height()) / (float) mHeight);
93
94 Entry* entry = new Entry(bitmap, x, y, rotated, mapper, *this);
95 entry->texture.id = mTexture;
96 entry->texture.blend = !bitmap->isOpaque();
97 entry->texture.width = bitmap->width();
98 entry->texture.height = bitmap->height();
99 entry->texture.uvMapper = &entry->uvMapper;
100
101 mEntries.add(entry->bitmap, entry);
102 }
103}
104
105}; // namespace uirenderer
106}; // namespace android