blob: faec5d28ad9225c487cb6ec3949079fc363b9831 [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
Romain Guy877cfe02013-05-02 17:36:28 -070037 mImage = new Image(buffer);
Romain Guy3b748a42013-04-17 18:54:38 -070038
Romain Guya404e162013-05-24 16:19:19 -070039 if (mImage->getTexture()) {
Romain Guy8aa195d2013-06-04 18:00:09 -070040 Caches& caches = Caches::getInstance();
41
42 mTexture = new Texture(caches);
Romain Guya404e162013-05-24 16:19:19 -070043 mTexture->id = mImage->getTexture();
44 mTexture->width = buffer->getWidth();
45 mTexture->height = buffer->getHeight();
Romain Guy3b748a42013-04-17 18:54:38 -070046
Romain Guy8aa195d2013-06-04 18:00:09 -070047 createEntries(caches, map, count);
Romain Guy877cfe02013-05-02 17:36:28 -070048 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070049 ALOGW("Could not create atlas image");
50
Romain Guy877cfe02013-05-02 17:36:28 -070051 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070052 mImage = NULL;
Romain Guya404e162013-05-24 16:19:19 -070053 mTexture = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070054 }
Romain Guy55b6f952013-06-27 15:27:09 -070055
56 mGenerationId++;
Romain Guy3b748a42013-04-17 18:54:38 -070057}
58
59void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070060 if (mImage) {
61 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070062 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070063
Romain Guya404e162013-05-24 16:19:19 -070064 delete mTexture;
65 mTexture = NULL;
66
Romain Guy3b748a42013-04-17 18:54:38 -070067 for (size_t i = 0; i < mEntries.size(); i++) {
68 delete mEntries.valueAt(i);
69 }
70 mEntries.clear();
Romain Guy3b748a42013-04-17 18:54:38 -070071 }
72}
73
74///////////////////////////////////////////////////////////////////////////////
75// Entries
76///////////////////////////////////////////////////////////////////////////////
77
Chris Craikd218a922014-01-02 17:13:34 -080078AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070079 ssize_t index = mEntries.indexOfKey(bitmap);
80 return index >= 0 ? mEntries.valueAt(index) : NULL;
81}
82
Chris Craikd218a922014-01-02 17:13:34 -080083Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070084 ssize_t index = mEntries.indexOfKey(bitmap);
Romain Guya404e162013-05-24 16:19:19 -070085 return index >= 0 ? mEntries.valueAt(index)->texture : NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070086}
87
88/**
Romain Guya404e162013-05-24 16:19:19 -070089 * Delegates changes to wrapping and filtering to the base atlas texture
90 * instead of applying the changes to the virtual textures.
91 */
92struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070093 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -070094
95 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
96 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
97 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
98 }
99
100 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
101 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
102 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
103 }
Romain Guy7f6d6b02013-08-06 13:49:28 -0700104
Romain Guya404e162013-05-24 16:19:19 -0700105private:
106 Texture* const mDelegate;
107}; // struct DelegateTexture
108
109/**
Romain Guy3b748a42013-04-17 18:54:38 -0700110 * TODO: This method does not take the rotation flag into account
111 */
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000112void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700113 const float width = float(mTexture->width);
114 const float height = float(mTexture->height);
115
Romain Guy3b748a42013-04-17 18:54:38 -0700116 for (int i = 0; i < count; ) {
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000117 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(map[i++]);
118 // NOTE: We're converting from 64 bit signed values to 32 bit
119 // signed values. This is guaranteed to be safe because the "x"
120 // and "y" coordinate values are guaranteed to be representable
121 // with 32 bits. The array is 64 bits wide so that it can carry
122 // pointers on 64 bit architectures.
123 const int x = static_cast<int>(map[i++]);
124 const int y = static_cast<int>(map[i++]);
Romain Guy3b748a42013-04-17 18:54:38 -0700125 bool rotated = map[i++] > 0;
126
127 // Bitmaps should never be null, we're just extra paranoid
128 if (!bitmap) continue;
129
130 const UvMapper mapper(
Romain Guya404e162013-05-24 16:19:19 -0700131 x / width, (x + bitmap->width()) / width,
132 y / height, (y + bitmap->height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700133
Romain Guy8aa195d2013-06-04 18:00:09 -0700134 Texture* texture = new DelegateTexture(caches, mTexture);
Romain Guya404e162013-05-24 16:19:19 -0700135 texture->id = mTexture->id;
136 texture->blend = !bitmap->isOpaque();
137 texture->width = bitmap->width();
138 texture->height = bitmap->height();
Romain Guy7f6d6b02013-08-06 13:49:28 -0700139
140 Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
Romain Guya404e162013-05-24 16:19:19 -0700141 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700142
143 mEntries.add(entry->bitmap, entry);
144 }
145}
146
147}; // namespace uirenderer
148}; // namespace android