blob: 41411a98a4bf5609f2d1e0fc043a65c5782192af [file] [log] [blame]
Romain Guy3b748a42013-04-17 18:54:38 -07001/*
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 Guy3b748a42013-04-17 18:54:38 -070017#include "AssetAtlas.h"
Romain Guy8aa195d2013-06-04 18:00:09 -070018#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040019#include "Image.h"
Romain Guy3b748a42013-04-17 18:54:38 -070020
21#include <GLES2/gl2ext.h>
22
Romain Guy3b748a42013-04-17 18:54:38 -070023namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Lifecycle
28///////////////////////////////////////////////////////////////////////////////
29
Ashok Bhat17ab38f2014-01-27 16:00:23 +000030void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) {
Romain Guy877cfe02013-05-02 17:36:28 -070031 if (mImage) {
Romain Guy3b748a42013-04-17 18:54:38 -070032 return;
33 }
34
John Reckfbc8df02014-11-14 16:18:41 -080035 ATRACE_NAME("AssetAtlas::init");
36
Romain Guy877cfe02013-05-02 17:36:28 -070037 mImage = new Image(buffer);
Romain Guya404e162013-05-24 16:19:19 -070038 if (mImage->getTexture()) {
John Reckebd52612014-12-10 16:47:36 -080039 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 Guy877cfe02013-05-02 17:36:28 -070046 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070047 ALOGW("Could not create atlas image");
Romain Guy877cfe02013-05-02 17:36:28 -070048 delete mImage;
Chris Craikd41c4d82015-01-05 15:51:13 -080049 mImage = nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070050 }
Romain Guy55b6f952013-06-27 15:27:09 -070051
John Reckebd52612014-12-10 16:47:36 -080052 updateTextureId();
Romain Guy3b748a42013-04-17 18:54:38 -070053}
54
55void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070056 if (mImage) {
57 delete mImage;
Chris Craikd41c4d82015-01-05 15:51:13 -080058 mImage = nullptr;
John Reckebd52612014-12-10 16:47:36 -080059 updateTextureId();
60 }
61}
Romain Guy3b748a42013-04-17 18:54:38 -070062
Romain Guya404e162013-05-24 16:19:19 -070063
John Reckebd52612014-12-10 16:47:36 -080064void AssetAtlas::updateTextureId() {
65 mTexture->id = mImage ? mImage->getTexture() : 0;
John Reck9a7fe1a2014-12-11 14:27:39 -080066 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 Reckebd52612014-12-10 16:47:36 -080072 for (size_t i = 0; i < mEntries.size(); i++) {
73 AssetAtlas::Entry* entry = mEntries.valueAt(i);
74 entry->texture->id = mTexture->id;
Romain Guy3b748a42013-04-17 18:54:38 -070075 }
76}
77
78///////////////////////////////////////////////////////////////////////////////
79// Entries
80///////////////////////////////////////////////////////////////////////////////
81
Chris Craik15c3f192015-12-03 12:16:56 -080082AssetAtlas::Entry* AssetAtlas::getEntry(const SkPixelRef* pixelRef) const {
83 ssize_t index = mEntries.indexOfKey(pixelRef);
Chris Craikd41c4d82015-01-05 15:51:13 -080084 return index >= 0 ? mEntries.valueAt(index) : nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070085}
86
Chris Craik15c3f192015-12-03 12:16:56 -080087Texture* AssetAtlas::getEntryTexture(const SkPixelRef* pixelRef) const {
88 ssize_t index = mEntries.indexOfKey(pixelRef);
Chris Craikd41c4d82015-01-05 15:51:13 -080089 return index >= 0 ? mEntries.valueAt(index)->texture : nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070090}
91
92/**
Romain Guya404e162013-05-24 16:19:19 -070093 * Delegates changes to wrapping and filtering to the base atlas texture
94 * instead of applying the changes to the virtual textures.
95 */
96struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070097 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -070098
99 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
Chris Craikd41c4d82015-01-05 15:51:13 -0800100 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
Romain Guya404e162013-05-24 16:19:19 -0700101 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
102 }
103
104 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
Chris Craikd41c4d82015-01-05 15:51:13 -0800105 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
Romain Guya404e162013-05-24 16:19:19 -0700106 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
107 }
Romain Guy7f6d6b02013-08-06 13:49:28 -0700108
Romain Guya404e162013-05-24 16:19:19 -0700109private:
110 Texture* const mDelegate;
111}; // struct DelegateTexture
112
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000113void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700114 const float width = float(mTexture->width);
115 const float height = float(mTexture->height);
116
Romain Guy3b748a42013-04-17 18:54:38 -0700117 for (int i = 0; i < count; ) {
John Reckc6e2e8f2015-04-15 13:24:47 -0700118 SkPixelRef* pixelRef = reinterpret_cast<SkPixelRef*>(map[i++]);
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000119 // 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 Guy3b748a42013-04-17 18:54:38 -0700126
127 // Bitmaps should never be null, we're just extra paranoid
John Reckc6e2e8f2015-04-15 13:24:47 -0700128 if (!pixelRef) continue;
Romain Guy3b748a42013-04-17 18:54:38 -0700129
130 const UvMapper mapper(
John Reckc6e2e8f2015-04-15 13:24:47 -0700131 x / width, (x + pixelRef->info().width()) / width,
132 y / height, (y + pixelRef->info().height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700133
Romain Guy8aa195d2013-06-04 18:00:09 -0700134 Texture* texture = new DelegateTexture(caches, mTexture);
John Reckc6e2e8f2015-04-15 13:24:47 -0700135 texture->blend = !SkAlphaTypeIsOpaque(pixelRef->info().alphaType());
136 texture->width = pixelRef->info().width();
137 texture->height = pixelRef->info().height();
Romain Guy7f6d6b02013-08-06 13:49:28 -0700138
John Recka0391822015-05-07 10:49:55 -0700139 Entry* entry = new Entry(pixelRef, texture, mapper, *this);
Romain Guya404e162013-05-24 16:19:19 -0700140 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700141
John Reckc6e2e8f2015-04-15 13:24:47 -0700142 mEntries.add(entry->pixelRef, entry);
Romain Guy3b748a42013-04-17 18:54:38 -0700143 }
144}
145
146}; // namespace uirenderer
147}; // namespace android