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" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 21 | #include "Image.h" |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 22 | |
| 23 | #include <GLES2/gl2ext.h> |
| 24 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Lifecycle |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 32 | void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) { |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 33 | if (mImage) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 37 | ATRACE_NAME("AssetAtlas::init"); |
| 38 | |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 39 | mImage = new Image(buffer); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 40 | if (mImage->getTexture()) { |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 41 | if (!mTexture) { |
| 42 | Caches& caches = Caches::getInstance(); |
| 43 | mTexture = new Texture(caches); |
| 44 | mTexture->width = buffer->getWidth(); |
| 45 | mTexture->height = buffer->getHeight(); |
| 46 | createEntries(caches, map, count); |
| 47 | } |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 48 | } else { |
Romain Guy | d5207b2 | 2013-05-07 14:46:36 -0700 | [diff] [blame] | 49 | ALOGW("Could not create atlas image"); |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 50 | delete mImage; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 51 | mImage = nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 52 | } |
Romain Guy | 55b6f95 | 2013-06-27 15:27:09 -0700 | [diff] [blame] | 53 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 54 | updateTextureId(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void AssetAtlas::terminate() { |
Romain Guy | 877cfe0 | 2013-05-02 17:36:28 -0700 | [diff] [blame] | 58 | if (mImage) { |
| 59 | delete mImage; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 60 | mImage = nullptr; |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 61 | updateTextureId(); |
| 62 | } |
| 63 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 64 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 65 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 66 | void AssetAtlas::updateTextureId() { |
| 67 | mTexture->id = mImage ? mImage->getTexture() : 0; |
John Reck | 9a7fe1a | 2014-12-11 14:27:39 -0800 | [diff] [blame] | 68 | if (mTexture->id) { |
| 69 | // Texture ID changed, force-set to defaults to sync the wrapper & GL |
| 70 | // state objects |
| 71 | mTexture->setWrap(GL_CLAMP_TO_EDGE, false, true); |
| 72 | mTexture->setFilter(GL_NEAREST, false, true); |
| 73 | } |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 74 | for (size_t i = 0; i < mEntries.size(); i++) { |
| 75 | AssetAtlas::Entry* entry = mEntries.valueAt(i); |
| 76 | entry->texture->id = mTexture->id; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | // Entries |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 84 | AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 85 | ssize_t index = mEntries.indexOfKey(bitmap); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 86 | return index >= 0 ? mEntries.valueAt(index) : nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 89 | Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 90 | ssize_t index = mEntries.indexOfKey(bitmap); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 91 | return index >= 0 ? mEntries.valueAt(index)->texture : nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 95 | * Delegates changes to wrapping and filtering to the base atlas texture |
| 96 | * instead of applying the changes to the virtual textures. |
| 97 | */ |
| 98 | struct DelegateTexture: public Texture { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 99 | DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { } |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 100 | |
| 101 | virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 102 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 103 | mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget); |
| 104 | } |
| 105 | |
| 106 | virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 107 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 108 | mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget); |
| 109 | } |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 110 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 111 | private: |
| 112 | Texture* const mDelegate; |
| 113 | }; // struct DelegateTexture |
| 114 | |
| 115 | /** |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 116 | * TODO: This method does not take the rotation flag into account |
| 117 | */ |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 118 | void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 119 | const float width = float(mTexture->width); |
| 120 | const float height = float(mTexture->height); |
| 121 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 122 | for (int i = 0; i < count; ) { |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 123 | SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(map[i++]); |
| 124 | // NOTE: We're converting from 64 bit signed values to 32 bit |
| 125 | // signed values. This is guaranteed to be safe because the "x" |
| 126 | // and "y" coordinate values are guaranteed to be representable |
| 127 | // with 32 bits. The array is 64 bits wide so that it can carry |
| 128 | // pointers on 64 bit architectures. |
| 129 | const int x = static_cast<int>(map[i++]); |
| 130 | const int y = static_cast<int>(map[i++]); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 131 | bool rotated = map[i++] > 0; |
| 132 | |
| 133 | // Bitmaps should never be null, we're just extra paranoid |
| 134 | if (!bitmap) continue; |
| 135 | |
| 136 | const UvMapper mapper( |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 137 | x / width, (x + bitmap->width()) / width, |
| 138 | y / height, (y + bitmap->height()) / height); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 139 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 140 | Texture* texture = new DelegateTexture(caches, mTexture); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 141 | texture->blend = !bitmap->isOpaque(); |
| 142 | texture->width = bitmap->width(); |
| 143 | texture->height = bitmap->height(); |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 144 | |
| 145 | Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 146 | texture->uvMapper = &entry->uvMapper; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 147 | |
| 148 | mEntries.add(entry->bitmap, entry); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | }; // namespace uirenderer |
| 153 | }; // namespace android |