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); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 34 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 35 | if (mImage->getTexture()) { |
| 36 | mTexture = new Texture(); |
| 37 | mTexture->id = mImage->getTexture(); |
| 38 | mTexture->width = buffer->getWidth(); |
| 39 | mTexture->height = buffer->getHeight(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 40 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 41 | createEntries(map, count); |
| 42 | } else { |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame] | 43 | ALOGW("Could not create atlas image"); |
| 44 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 45 | delete mImage; |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame] | 46 | mImage = NULL; |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 47 | mTexture = NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 48 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void AssetAtlas::terminate() { |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 52 | if (mImage) { |
| 53 | delete mImage; |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame] | 54 | mImage = NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 55 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 56 | delete mTexture; |
| 57 | mTexture = NULL; |
| 58 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 59 | for (size_t i = 0; i < mEntries.size(); i++) { |
| 60 | delete mEntries.valueAt(i); |
| 61 | } |
| 62 | mEntries.clear(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | // Entries |
| 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | |
| 70 | AssetAtlas::Entry* AssetAtlas::getEntry(SkBitmap* const bitmap) const { |
| 71 | ssize_t index = mEntries.indexOfKey(bitmap); |
| 72 | return index >= 0 ? mEntries.valueAt(index) : NULL; |
| 73 | } |
| 74 | |
| 75 | Texture* AssetAtlas::getEntryTexture(SkBitmap* const bitmap) const { |
| 76 | ssize_t index = mEntries.indexOfKey(bitmap); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 77 | return index >= 0 ? mEntries.valueAt(index)->texture : NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 81 | * Delegates changes to wrapping and filtering to the base atlas texture |
| 82 | * instead of applying the changes to the virtual textures. |
| 83 | */ |
| 84 | struct DelegateTexture: public Texture { |
| 85 | DelegateTexture(Texture* delegate): Texture(), mDelegate(delegate) { } |
| 86 | |
| 87 | virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, |
| 88 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) { |
| 89 | mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget); |
| 90 | } |
| 91 | |
| 92 | virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, |
| 93 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) { |
| 94 | mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget); |
| 95 | } |
| 96 | private: |
| 97 | Texture* const mDelegate; |
| 98 | }; // struct DelegateTexture |
| 99 | |
| 100 | /** |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 101 | * TODO: This method does not take the rotation flag into account |
| 102 | */ |
| 103 | void AssetAtlas::createEntries(int* map, int count) { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 104 | const float width = float(mTexture->width); |
| 105 | const float height = float(mTexture->height); |
| 106 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 107 | for (int i = 0; i < count; ) { |
| 108 | SkBitmap* bitmap = (SkBitmap*) map[i++]; |
| 109 | int x = map[i++]; |
| 110 | int y = map[i++]; |
| 111 | bool rotated = map[i++] > 0; |
| 112 | |
| 113 | // Bitmaps should never be null, we're just extra paranoid |
| 114 | if (!bitmap) continue; |
| 115 | |
| 116 | const UvMapper mapper( |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 117 | x / width, (x + bitmap->width()) / width, |
| 118 | y / height, (y + bitmap->height()) / height); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 119 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame^] | 120 | Texture* texture = new DelegateTexture(mTexture); |
| 121 | Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this); |
| 122 | |
| 123 | texture->id = mTexture->id; |
| 124 | texture->blend = !bitmap->isOpaque(); |
| 125 | texture->width = bitmap->width(); |
| 126 | texture->height = bitmap->height(); |
| 127 | texture->uvMapper = &entry->uvMapper; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 128 | |
| 129 | mEntries.add(entry->bitmap, entry); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | }; // namespace uirenderer |
| 134 | }; // namespace android |