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