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