blob: 782c052c193ded9e1d0e0f4baf2cbc48e6da36d0 [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"
Romain Guy3b748a42013-04-17 18:54:38 -070019
20#include <GLES2/gl2ext.h>
21
Romain Guy3b748a42013-04-17 18:54:38 -070022namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
26// Lifecycle
27///////////////////////////////////////////////////////////////////////////////
28
29void AssetAtlas::init(sp<GraphicBuffer> buffer, int* map, int count) {
Romain Guy877cfe02013-05-02 17:36:28 -070030 if (mImage) {
Romain Guy3b748a42013-04-17 18:54:38 -070031 return;
32 }
33
Romain Guy877cfe02013-05-02 17:36:28 -070034 mImage = new Image(buffer);
Romain Guy3b748a42013-04-17 18:54:38 -070035
Romain Guya404e162013-05-24 16:19:19 -070036 if (mImage->getTexture()) {
Romain Guy8aa195d2013-06-04 18:00:09 -070037 Caches& caches = Caches::getInstance();
38
39 mTexture = new Texture(caches);
Romain Guya404e162013-05-24 16:19:19 -070040 mTexture->id = mImage->getTexture();
41 mTexture->width = buffer->getWidth();
42 mTexture->height = buffer->getHeight();
Romain Guy3b748a42013-04-17 18:54:38 -070043
Romain Guy8aa195d2013-06-04 18:00:09 -070044 createEntries(caches, map, count);
Romain Guy877cfe02013-05-02 17:36:28 -070045 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070046 ALOGW("Could not create atlas image");
47
Romain Guy877cfe02013-05-02 17:36:28 -070048 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070049 mImage = NULL;
Romain Guya404e162013-05-24 16:19:19 -070050 mTexture = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070051 }
Romain Guy3b748a42013-04-17 18:54:38 -070052}
53
54void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070055 if (mImage) {
56 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070057 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070058
Romain Guya404e162013-05-24 16:19:19 -070059 delete mTexture;
60 mTexture = NULL;
61
Romain Guy3b748a42013-04-17 18:54:38 -070062 for (size_t i = 0; i < mEntries.size(); i++) {
63 delete mEntries.valueAt(i);
64 }
65 mEntries.clear();
Romain Guy3b748a42013-04-17 18:54:38 -070066 }
67}
68
69///////////////////////////////////////////////////////////////////////////////
70// Entries
71///////////////////////////////////////////////////////////////////////////////
72
73AssetAtlas::Entry* AssetAtlas::getEntry(SkBitmap* const bitmap) const {
74 ssize_t index = mEntries.indexOfKey(bitmap);
75 return index >= 0 ? mEntries.valueAt(index) : NULL;
76}
77
78Texture* AssetAtlas::getEntryTexture(SkBitmap* const bitmap) const {
79 ssize_t index = mEntries.indexOfKey(bitmap);
Romain Guya404e162013-05-24 16:19:19 -070080 return index >= 0 ? mEntries.valueAt(index)->texture : NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070081}
82
83/**
Romain Guya404e162013-05-24 16:19:19 -070084 * Delegates changes to wrapping and filtering to the base atlas texture
85 * instead of applying the changes to the virtual textures.
86 */
87struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070088 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -070089
90 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
91 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
92 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
93 }
94
95 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
96 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
97 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
98 }
99private:
100 Texture* const mDelegate;
101}; // struct DelegateTexture
102
103/**
Romain Guy3b748a42013-04-17 18:54:38 -0700104 * TODO: This method does not take the rotation flag into account
105 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700106void AssetAtlas::createEntries(Caches& caches, int* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700107 const float width = float(mTexture->width);
108 const float height = float(mTexture->height);
109
Romain Guy3b748a42013-04-17 18:54:38 -0700110 for (int i = 0; i < count; ) {
111 SkBitmap* bitmap = (SkBitmap*) map[i++];
112 int x = map[i++];
113 int y = map[i++];
114 bool rotated = map[i++] > 0;
115
116 // Bitmaps should never be null, we're just extra paranoid
117 if (!bitmap) continue;
118
119 const UvMapper mapper(
Romain Guya404e162013-05-24 16:19:19 -0700120 x / width, (x + bitmap->width()) / width,
121 y / height, (y + bitmap->height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700122
Romain Guy8aa195d2013-06-04 18:00:09 -0700123 Texture* texture = new DelegateTexture(caches, mTexture);
Romain Guya404e162013-05-24 16:19:19 -0700124 Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
125
126 texture->id = mTexture->id;
127 texture->blend = !bitmap->isOpaque();
128 texture->width = bitmap->width();
129 texture->height = bitmap->height();
130 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700131
132 mEntries.add(entry->bitmap, entry);
133 }
134}
135
136}; // namespace uirenderer
137}; // namespace android