Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 17 | #include "AssetAtlas.h" |
| 18 | |
| 19 | #include <GLES2/gl2ext.h> |
| 20 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Lifecycle |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | void AssetAtlas::init(sp<GraphicBuffer> buffer, int* map, int count) { |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 29 | if (mImage) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 30 | return; |
| 31 | } |
| 32 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 33 | mImage = new Image(buffer); |
| 34 | mTexture = mImage->getTexture(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 35 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 36 | if (mTexture) { |
| 37 | mWidth = buffer->getWidth(); |
| 38 | mHeight = buffer->getHeight(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 39 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 40 | createEntries(map, count); |
| 41 | } else { |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame^] | 42 | ALOGW("Could not create atlas image"); |
| 43 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 44 | delete mImage; |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame^] | 45 | mImage = NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 46 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void AssetAtlas::terminate() { |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 50 | if (mImage) { |
| 51 | delete mImage; |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame^] | 52 | mImage = NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 53 | |
| 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 | |
| 67 | AssetAtlas::Entry* AssetAtlas::getEntry(SkBitmap* const bitmap) const { |
| 68 | ssize_t index = mEntries.indexOfKey(bitmap); |
| 69 | return index >= 0 ? mEntries.valueAt(index) : NULL; |
| 70 | } |
| 71 | |
| 72 | Texture* 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 | */ |
| 80 | void 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 |