blob: fddfe9064ab1cfdea491745ee93a47b7457b2454 [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 Guy55b6f952013-06-27 15:27:09 -070017#define LOG_TAG "OpenGLRenderer"
18
Romain Guy3b748a42013-04-17 18:54:38 -070019#include "AssetAtlas.h"
Romain Guy8aa195d2013-06-04 18:00:09 -070020#include "Caches.h"
Romain Guy3b748a42013-04-17 18:54:38 -070021
22#include <GLES2/gl2ext.h>
23
Romain Guy3b748a42013-04-17 18:54:38 -070024namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Lifecycle
29///////////////////////////////////////////////////////////////////////////////
30
31void AssetAtlas::init(sp<GraphicBuffer> buffer, int* map, int count) {
Romain Guy877cfe02013-05-02 17:36:28 -070032 if (mImage) {
Romain Guy3b748a42013-04-17 18:54:38 -070033 return;
34 }
35
Romain Guy877cfe02013-05-02 17:36:28 -070036 mImage = new Image(buffer);
Romain Guy3b748a42013-04-17 18:54:38 -070037
Romain Guya404e162013-05-24 16:19:19 -070038 if (mImage->getTexture()) {
Romain Guy8aa195d2013-06-04 18:00:09 -070039 Caches& caches = Caches::getInstance();
40
41 mTexture = new Texture(caches);
Romain Guya404e162013-05-24 16:19:19 -070042 mTexture->id = mImage->getTexture();
43 mTexture->width = buffer->getWidth();
44 mTexture->height = buffer->getHeight();
Romain Guy3b748a42013-04-17 18:54:38 -070045
Romain Guy8aa195d2013-06-04 18:00:09 -070046 createEntries(caches, map, count);
Romain Guy877cfe02013-05-02 17:36:28 -070047 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070048 ALOGW("Could not create atlas image");
49
Romain Guy877cfe02013-05-02 17:36:28 -070050 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070051 mImage = NULL;
Romain Guya404e162013-05-24 16:19:19 -070052 mTexture = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070053 }
Romain Guy55b6f952013-06-27 15:27:09 -070054
55 mGenerationId++;
Romain Guy3b748a42013-04-17 18:54:38 -070056}
57
58void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070059 if (mImage) {
60 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070061 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070062
Romain Guya404e162013-05-24 16:19:19 -070063 delete mTexture;
64 mTexture = NULL;
65
Romain Guy3b748a42013-04-17 18:54:38 -070066 for (size_t i = 0; i < mEntries.size(); i++) {
67 delete mEntries.valueAt(i);
68 }
69 mEntries.clear();
Romain Guy3b748a42013-04-17 18:54:38 -070070 }
71}
72
73///////////////////////////////////////////////////////////////////////////////
74// Entries
75///////////////////////////////////////////////////////////////////////////////
76
Chris Craikd218a922014-01-02 17:13:34 -080077AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070078 ssize_t index = mEntries.indexOfKey(bitmap);
79 return index >= 0 ? mEntries.valueAt(index) : NULL;
80}
81
Chris Craikd218a922014-01-02 17:13:34 -080082Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070083 ssize_t index = mEntries.indexOfKey(bitmap);
Romain Guya404e162013-05-24 16:19:19 -070084 return index >= 0 ? mEntries.valueAt(index)->texture : NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070085}
86
87/**
Romain Guya404e162013-05-24 16:19:19 -070088 * Delegates changes to wrapping and filtering to the base atlas texture
89 * instead of applying the changes to the virtual textures.
90 */
91struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070092 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -070093
94 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
95 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
96 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
97 }
98
99 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
100 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
101 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
102 }
Romain Guy7f6d6b02013-08-06 13:49:28 -0700103
Romain Guya404e162013-05-24 16:19:19 -0700104private:
105 Texture* const mDelegate;
106}; // struct DelegateTexture
107
108/**
Romain Guy3b748a42013-04-17 18:54:38 -0700109 * TODO: This method does not take the rotation flag into account
110 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700111void AssetAtlas::createEntries(Caches& caches, int* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700112 const float width = float(mTexture->width);
113 const float height = float(mTexture->height);
114
Romain Guy3b748a42013-04-17 18:54:38 -0700115 for (int i = 0; i < count; ) {
116 SkBitmap* bitmap = (SkBitmap*) map[i++];
117 int x = map[i++];
118 int y = map[i++];
119 bool rotated = map[i++] > 0;
120
121 // Bitmaps should never be null, we're just extra paranoid
122 if (!bitmap) continue;
123
124 const UvMapper mapper(
Romain Guya404e162013-05-24 16:19:19 -0700125 x / width, (x + bitmap->width()) / width,
126 y / height, (y + bitmap->height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700127
Romain Guy8aa195d2013-06-04 18:00:09 -0700128 Texture* texture = new DelegateTexture(caches, mTexture);
Romain Guya404e162013-05-24 16:19:19 -0700129 texture->id = mTexture->id;
130 texture->blend = !bitmap->isOpaque();
131 texture->width = bitmap->width();
132 texture->height = bitmap->height();
Romain Guy7f6d6b02013-08-06 13:49:28 -0700133
134 Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
Romain Guya404e162013-05-24 16:19:19 -0700135 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700136
137 mEntries.add(entry->bitmap, entry);
138 }
139}
140
141}; // namespace uirenderer
142}; // namespace android