blob: 4d2e3a097b5514ceefeb56a1483ba7fd3d69ddf7 [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 Guya404e162013-05-24 16:19:19 -070040 if (mImage->getTexture()) {
John Reckebd52612014-12-10 16:47:36 -080041 if (!mTexture) {
42 Caches& caches = Caches::getInstance();
43 mTexture = new Texture(caches);
44 mTexture->width = buffer->getWidth();
45 mTexture->height = buffer->getHeight();
46 createEntries(caches, map, count);
47 }
Romain Guy877cfe02013-05-02 17:36:28 -070048 } else {
Romain Guyd5207b22013-05-07 14:46:36 -070049 ALOGW("Could not create atlas image");
Romain Guy877cfe02013-05-02 17:36:28 -070050 delete mImage;
Chris Craikd41c4d82015-01-05 15:51:13 -080051 mImage = nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070052 }
Romain Guy55b6f952013-06-27 15:27:09 -070053
John Reckebd52612014-12-10 16:47:36 -080054 updateTextureId();
Romain Guy3b748a42013-04-17 18:54:38 -070055}
56
57void AssetAtlas::terminate() {
Romain Guy877cfe02013-05-02 17:36:28 -070058 if (mImage) {
59 delete mImage;
Chris Craikd41c4d82015-01-05 15:51:13 -080060 mImage = nullptr;
John Reckebd52612014-12-10 16:47:36 -080061 updateTextureId();
62 }
63}
Romain Guy3b748a42013-04-17 18:54:38 -070064
Romain Guya404e162013-05-24 16:19:19 -070065
John Reckebd52612014-12-10 16:47:36 -080066void AssetAtlas::updateTextureId() {
67 mTexture->id = mImage ? mImage->getTexture() : 0;
John Reck9a7fe1a2014-12-11 14:27:39 -080068 if (mTexture->id) {
69 // Texture ID changed, force-set to defaults to sync the wrapper & GL
70 // state objects
71 mTexture->setWrap(GL_CLAMP_TO_EDGE, false, true);
72 mTexture->setFilter(GL_NEAREST, false, true);
73 }
John Reckebd52612014-12-10 16:47:36 -080074 for (size_t i = 0; i < mEntries.size(); i++) {
75 AssetAtlas::Entry* entry = mEntries.valueAt(i);
76 entry->texture->id = mTexture->id;
Romain Guy3b748a42013-04-17 18:54:38 -070077 }
78}
79
80///////////////////////////////////////////////////////////////////////////////
81// Entries
82///////////////////////////////////////////////////////////////////////////////
83
Chris Craikd218a922014-01-02 17:13:34 -080084AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
John Reck7809f832015-04-17 20:45:19 +000085 ssize_t index = mEntries.indexOfKey(bitmap);
Chris Craikd41c4d82015-01-05 15:51:13 -080086 return index >= 0 ? mEntries.valueAt(index) : nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070087}
88
Chris Craikd218a922014-01-02 17:13:34 -080089Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
John Reck7809f832015-04-17 20:45:19 +000090 ssize_t index = mEntries.indexOfKey(bitmap);
Chris Craikd41c4d82015-01-05 15:51:13 -080091 return index >= 0 ? mEntries.valueAt(index)->texture : nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070092}
93
94/**
Romain Guya404e162013-05-24 16:19:19 -070095 * Delegates changes to wrapping and filtering to the base atlas texture
96 * instead of applying the changes to the virtual textures.
97 */
98struct DelegateTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070099 DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
Romain Guya404e162013-05-24 16:19:19 -0700100
101 virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
Chris Craikd41c4d82015-01-05 15:51:13 -0800102 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
Romain Guya404e162013-05-24 16:19:19 -0700103 mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
104 }
105
106 virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
Chris Craikd41c4d82015-01-05 15:51:13 -0800107 bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
Romain Guya404e162013-05-24 16:19:19 -0700108 mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
109 }
Romain Guy7f6d6b02013-08-06 13:49:28 -0700110
Romain Guya404e162013-05-24 16:19:19 -0700111private:
112 Texture* const mDelegate;
113}; // struct DelegateTexture
114
115/**
Romain Guy3b748a42013-04-17 18:54:38 -0700116 * TODO: This method does not take the rotation flag into account
117 */
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000118void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
Romain Guya404e162013-05-24 16:19:19 -0700119 const float width = float(mTexture->width);
120 const float height = float(mTexture->height);
121
Romain Guy3b748a42013-04-17 18:54:38 -0700122 for (int i = 0; i < count; ) {
John Reck7809f832015-04-17 20:45:19 +0000123 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(map[i++]);
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000124 // NOTE: We're converting from 64 bit signed values to 32 bit
125 // signed values. This is guaranteed to be safe because the "x"
126 // and "y" coordinate values are guaranteed to be representable
127 // with 32 bits. The array is 64 bits wide so that it can carry
128 // pointers on 64 bit architectures.
129 const int x = static_cast<int>(map[i++]);
130 const int y = static_cast<int>(map[i++]);
Romain Guy3b748a42013-04-17 18:54:38 -0700131 bool rotated = map[i++] > 0;
132
133 // Bitmaps should never be null, we're just extra paranoid
John Reck7809f832015-04-17 20:45:19 +0000134 if (!bitmap) continue;
Romain Guy3b748a42013-04-17 18:54:38 -0700135
136 const UvMapper mapper(
John Reck7809f832015-04-17 20:45:19 +0000137 x / width, (x + bitmap->width()) / width,
138 y / height, (y + bitmap->height()) / height);
Romain Guy3b748a42013-04-17 18:54:38 -0700139
Romain Guy8aa195d2013-06-04 18:00:09 -0700140 Texture* texture = new DelegateTexture(caches, mTexture);
John Reck7809f832015-04-17 20:45:19 +0000141 texture->blend = !bitmap->isOpaque();
142 texture->width = bitmap->width();
143 texture->height = bitmap->height();
Romain Guy7f6d6b02013-08-06 13:49:28 -0700144
John Reck7809f832015-04-17 20:45:19 +0000145 Entry* entry = new Entry(bitmap, x, y, rotated, texture, mapper, *this);
Romain Guya404e162013-05-24 16:19:19 -0700146 texture->uvMapper = &entry->uvMapper;
Romain Guy3b748a42013-04-17 18:54:38 -0700147
John Reck7809f832015-04-17 20:45:19 +0000148 mEntries.add(entry->bitmap, entry);
Romain Guy3b748a42013-04-17 18:54:38 -0700149 }
150}
151
152}; // namespace uirenderer
153}; // namespace android