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); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 42 | mTexture->wrap(mImage->getTexture(), |
| 43 | buffer->getWidth(), buffer->getHeight(), GL_RGBA); |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 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"); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 48 | terminate(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 49 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void AssetAtlas::terminate() { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 53 | delete mImage; |
| 54 | mImage = nullptr; |
| 55 | delete mTexture; |
| 56 | mTexture = nullptr; |
| 57 | mEntries.clear(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | // Entries |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 64 | AssetAtlas::Entry* AssetAtlas::getEntry(const SkPixelRef* pixelRef) const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 65 | auto result = mEntries.find(pixelRef); |
| 66 | return result != mEntries.end() ? result->second.get() : nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 69 | Texture* AssetAtlas::getEntryTexture(const SkPixelRef* pixelRef) const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 70 | auto result = mEntries.find(pixelRef); |
| 71 | return result != mEntries.end() ? result->second->texture : nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 75 | * Delegates changes to wrapping and filtering to the base atlas texture |
| 76 | * instead of applying the changes to the virtual textures. |
| 77 | */ |
| 78 | struct DelegateTexture: public Texture { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 79 | DelegateTexture(Caches& caches, Texture* delegate) |
| 80 | : Texture(caches), mDelegate(delegate) { } |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 81 | |
| 82 | virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false, |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 83 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 84 | mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget); |
| 85 | } |
| 86 | |
| 87 | virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false, |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 88 | bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override { |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 89 | mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget); |
| 90 | } |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 91 | |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 92 | private: |
| 93 | Texture* const mDelegate; |
| 94 | }; // struct DelegateTexture |
| 95 | |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 96 | void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 97 | const float width = float(mTexture->width()); |
| 98 | const float height = float(mTexture->height()); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 99 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 100 | for (int i = 0; i < count; ) { |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 101 | SkPixelRef* pixelRef = reinterpret_cast<SkPixelRef*>(map[i++]); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 102 | // NOTE: We're converting from 64 bit signed values to 32 bit |
| 103 | // signed values. This is guaranteed to be safe because the "x" |
| 104 | // and "y" coordinate values are guaranteed to be representable |
| 105 | // with 32 bits. The array is 64 bits wide so that it can carry |
| 106 | // pointers on 64 bit architectures. |
| 107 | const int x = static_cast<int>(map[i++]); |
| 108 | const int y = static_cast<int>(map[i++]); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 109 | |
| 110 | // Bitmaps should never be null, we're just extra paranoid |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 111 | if (!pixelRef) continue; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 112 | |
| 113 | const UvMapper mapper( |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 114 | x / width, (x + pixelRef->info().width()) / width, |
| 115 | y / height, (y + pixelRef->info().height()) / height); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 116 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 117 | Texture* texture = new DelegateTexture(caches, mTexture); |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 118 | texture->blend = !SkAlphaTypeIsOpaque(pixelRef->info().alphaType()); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 119 | texture->wrap(mTexture->id(), pixelRef->info().width(), |
| 120 | pixelRef->info().height(), mTexture->format()); |
Romain Guy | 7f6d6b0 | 2013-08-06 13:49:28 -0700 | [diff] [blame] | 121 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 122 | std::unique_ptr<Entry> entry(new Entry(pixelRef, texture, mapper, *this)); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 123 | texture->uvMapper = &entry->uvMapper; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 124 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 125 | mEntries.emplace(entry->pixelRef, std::move(entry)); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | }; // namespace uirenderer |
| 130 | }; // namespace android |