blob: fa7635e235961ec296809a32f6c26a3ef9537f76 [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"
Tom Hudson2dc236b2014-10-15 15:46:42 -040021#include "Image.h"
Romain Guy3b748a42013-04-17 18:54:38 -070022
23#include <GLES2/gl2ext.h>
24
Romain Guy3b748a42013-04-17 18:54:38 -070025namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Lifecycle
30///////////////////////////////////////////////////////////////////////////////
31
Ashok Bhat17ab38f2014-01-27 16:00:23 +000032void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) {
Romain Guy877cfe02013-05-02 17:36:28 -070033 if (mImage) {
Romain Guy3b748a42013-04-17 18:54:38 -070034 return;
35 }
36
John Reckfbc8df02014-11-14 16:18:41 -080037 ATRACE_NAME("AssetAtlas::init");
38
Romain Guy877cfe02013-05-02 17:36:28 -070039 mImage = new Image(buffer);
Romain Guy3b748a42013-04-17 18:54:38 -070040
Romain Guya404e162013-05-24 16:19:19 -070041 if (mImage->getTexture()) {
Romain Guy8aa195d2013-06-04 18:00:09 -070042 Caches& caches = Caches::getInstance();
43
44 mTexture = new Texture(caches);
Romain Guya404e162013-05-24 16:19:19 -070045 mTexture->id = mImage->getTexture();
46 mTexture->width = buffer->getWidth();
47 mTexture->height = buffer->getHeight();
Romain Guy3b748a42013-04-17 18:54:38 -070048
Romain Guy8aa195d2013-06-04 18:00:09 -070049 createEntries(caches, map, count);
Romain Guy877cfe02013-05-02 17:36:28 -070050 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070051 ALOGW("Could not create atlas image");
52
Romain Guy877cfe02013-05-02 17:36:28 -070053 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070054 mImage = NULL;
Romain Guya404e162013-05-24 16:19:19 -070055 mTexture = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070056 }
Romain Guy55b6f952013-06-27 15:27:09 -070057
58 mGenerationId++;
Romain Guy3b748a42013-04-17 18:54:38 -070059}
60
61void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070062 if (mImage) {
63 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070064 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070065
Romain Guya404e162013-05-24 16:19:19 -070066 delete mTexture;
67 mTexture = NULL;
68
Romain Guy3b748a42013-04-17 18:54:38 -070069 for (size_t i = 0; i < mEntries.size(); i++) {
70 delete mEntries.valueAt(i);
71 }
72 mEntries.clear();
Romain Guy3b748a42013-04-17 18:54:38 -070073 }
74}
75
76///////////////////////////////////////////////////////////////////////////////
77// Entries
78///////////////////////////////////////////////////////////////////////////////
79
Chris Craikd218a922014-01-02 17:13:34 -080080AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070081 ssize_t index = mEntries.indexOfKey(bitmap);
82 return index >= 0 ? mEntries.valueAt(index) : NULL;
83}
84
Chris Craikd218a922014-01-02 17:13:34 -080085Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070086 ssize_t index = mEntries.indexOfKey(bitmap);
Romain Guya404e162013-05-24 16:19:19 -070087 return index >= 0 ? mEntries.valueAt(index)->texture : NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070088}
89
90/**
Romain Guya404e162013-05-24 16:19:19 -070091 * Delegates changes to wrapping and filtering to the base atlas texture
92 * instead of applying the changes to the virtual textures.
93 */
94struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070095 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -070096
97 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
98 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
99 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
100 }
101
102 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
103 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
104 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
105 }
Romain Guy7f6d6b02013-08-06 13:49:28 -0700106
Romain Guya404e162013-05-24 16:19:19 -0700107private:
108 Texture* const mDelegate;
109}; // struct DelegateTexture
110
111/**
Romain Guy3b748a42013-04-17 18:54:38 -0700112 * TODO: This method does not take the rotation flag into account
113 */
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000114void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700115 const float width = float(mTexture->width);
116 const float height = float(mTexture->height);
117
Romain Guy3b748a42013-04-17 18:54:38 -0700118 for (int i = 0; i < count; ) {
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000119 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(map[i++]);
120 // NOTE: We're converting from 64 bit signed values to 32 bit
121 // signed values. This is guaranteed to be safe because the "x"
122 // and "y" coordinate values are guaranteed to be representable
123 // with 32 bits. The array is 64 bits wide so that it can carry
124 // pointers on 64 bit architectures.
125 const int x = static_cast<int>(map[i++]);
126 const int y = static_cast<int>(map[i++]);
Romain Guy3b748a42013-04-17 18:54:38 -0700127 bool rotated = map[i++] > 0;
128
129 // Bitmaps should never be null, we're just extra paranoid
130 if (!bitmap) continue;
131
132 const UvMapper mapper(
Romain Guya404e162013-05-24 16:19:19 -0700133 x / width, (x + bitmap->width()) / width,
134 y / height, (y + bitmap->height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700135
Romain Guy8aa195d2013-06-04 18:00:09 -0700136 Texture* texture = new DelegateTexture(caches, mTexture);
Romain Guya404e162013-05-24 16:19:19 -0700137 texture->id = mTexture->id;
138 texture->blend = !bitmap->isOpaque();
139 texture->width = bitmap->width();
140 texture->height = bitmap->height();
Romain Guy7f6d6b02013-08-06 13:49:28 -0700141
142 Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
Romain Guya404e162013-05-24 16:19:19 -0700143 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700144
145 mEntries.add(entry->bitmap, entry);
146 }
147}
148
149}; // namespace uirenderer
150}; // namespace android