blob: 52ca92dcd388088f39e1c85e5d0374e3789ad68d [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
Ashok Bhat17ab38f2014-01-27 16:00:23 +000031void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) {
Romain Guy877cfe02013-05-02 17:36:28 -070032 if (mImage) {
Romain Guy3b748a42013-04-17 18:54:38 -070033 return;
34 }
35
John Reckfbc8df02014-11-14 16:18:41 -080036 ATRACE_NAME("AssetAtlas::init");
37
Romain Guy877cfe02013-05-02 17:36:28 -070038 mImage = new Image(buffer);
Romain Guya404e162013-05-24 16:19:19 -070039 if (mImage->getTexture()) {
John Reckebd52612014-12-10 16:47:36 -080040 if (!mTexture) {
41 Caches& caches = Caches::getInstance();
42 mTexture = new Texture(caches);
43 mTexture->width = buffer->getWidth();
44 mTexture->height = buffer->getHeight();
45 createEntries(caches, map, count);
46 }
Romain Guy877cfe02013-05-02 17:36:28 -070047 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070048 ALOGW("Could not create atlas image");
Romain Guy877cfe02013-05-02 17:36:28 -070049 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070050 mImage = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070051 }
Romain Guy55b6f952013-06-27 15:27:09 -070052
John Reckebd52612014-12-10 16:47:36 -080053 updateTextureId();
Romain Guy3b748a42013-04-17 18:54:38 -070054}
55
56void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070057 if (mImage) {
58 delete mImage;
Romain Guyd5207b22013-05-07 14:46:36 -070059 mImage = NULL;
John Reckebd52612014-12-10 16:47:36 -080060 updateTextureId();
61 }
62}
Romain Guy3b748a42013-04-17 18:54:38 -070063
Romain Guya404e162013-05-24 16:19:19 -070064
John Reckebd52612014-12-10 16:47:36 -080065void AssetAtlas::updateTextureId() {
66 mTexture->id = mImage ? mImage->getTexture() : 0;
John Reck9a7fe1a2014-12-11 14:27:39 -080067 if (mTexture->id) {
68 // Texture ID changed, force-set to defaults to sync the wrapper & GL
69 // state objects
70 mTexture->setWrap(GL_CLAMP_TO_EDGE, false, true);
71 mTexture->setFilter(GL_NEAREST, false, true);
72 }
John Reckebd52612014-12-10 16:47:36 -080073 for (size_t i = 0; i < mEntries.size(); i++) {
74 AssetAtlas::Entry* entry = mEntries.valueAt(i);
75 entry->texture->id = mTexture->id;
Romain Guy3b748a42013-04-17 18:54:38 -070076 }
77}
78
79///////////////////////////////////////////////////////////////////////////////
80// Entries
81///////////////////////////////////////////////////////////////////////////////
82
Chris Craikd218a922014-01-02 17:13:34 -080083AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070084 ssize_t index = mEntries.indexOfKey(bitmap);
85 return index >= 0 ? mEntries.valueAt(index) : NULL;
86}
87
Chris Craikd218a922014-01-02 17:13:34 -080088Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
Romain Guy3b748a42013-04-17 18:54:38 -070089 ssize_t index = mEntries.indexOfKey(bitmap);
Romain Guya404e162013-05-24 16:19:19 -070090 return index >= 0 ? mEntries.valueAt(index)->texture : NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070091}
92
93/**
Romain Guya404e162013-05-24 16:19:19 -070094 * Delegates changes to wrapping and filtering to the base atlas texture
95 * instead of applying the changes to the virtual textures.
96 */
97struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070098 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -070099
100 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
101 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
102 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
103 }
104
105 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
106 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) {
107 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
108 }
Romain Guy7f6d6b02013-08-06 13:49:28 -0700109
Romain Guya404e162013-05-24 16:19:19 -0700110private:
111 Texture* const mDelegate;
112}; // struct DelegateTexture
113
114/**
Romain Guy3b748a42013-04-17 18:54:38 -0700115 * TODO: This method does not take the rotation flag into account
116 */
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000117void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700118 const float width = float(mTexture->width);
119 const float height = float(mTexture->height);
120
Romain Guy3b748a42013-04-17 18:54:38 -0700121 for (int i = 0; i < count; ) {
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000122 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(map[i++]);
123 // NOTE: We're converting from 64 bit signed values to 32 bit
124 // signed values. This is guaranteed to be safe because the "x"
125 // and "y" coordinate values are guaranteed to be representable
126 // with 32 bits. The array is 64 bits wide so that it can carry
127 // pointers on 64 bit architectures.
128 const int x = static_cast<int>(map[i++]);
129 const int y = static_cast<int>(map[i++]);
Romain Guy3b748a42013-04-17 18:54:38 -0700130 bool rotated = map[i++] > 0;
131
132 // Bitmaps should never be null, we're just extra paranoid
133 if (!bitmap) continue;
134
135 const UvMapper mapper(
Romain Guya404e162013-05-24 16:19:19 -0700136 x / width, (x + bitmap->width()) / width,
137 y / height, (y + bitmap->height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700138
Romain Guy8aa195d2013-06-04 18:00:09 -0700139 Texture* texture = new DelegateTexture(caches, mTexture);
Romain Guya404e162013-05-24 16:19:19 -0700140 texture->blend = !bitmap->isOpaque();
141 texture->width = bitmap->width();
142 texture->height = bitmap->height();
Romain Guy7f6d6b02013-08-06 13:49:28 -0700143
144 Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
Romain Guya404e162013-05-24 16:19:19 -0700145 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700146
147 mEntries.add(entry->bitmap, entry);
148 }
149}
150
151}; // namespace uirenderer
152}; // namespace android