• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #define LOG_TAG "OpenGLRenderer"
18 
19 #include "AssetAtlas.h"
20 #include "Caches.h"
21 #include "Image.h"
22 
23 #include <GLES2/gl2ext.h>
24 
25 namespace android {
26 namespace uirenderer {
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 // Lifecycle
30 ///////////////////////////////////////////////////////////////////////////////
31 
init(sp<GraphicBuffer> buffer,int64_t * map,int count)32 void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) {
33     if (mImage) {
34         return;
35     }
36 
37     ATRACE_NAME("AssetAtlas::init");
38 
39     mImage = new Image(buffer);
40     if (mImage->getTexture()) {
41         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         }
48     } else {
49         ALOGW("Could not create atlas image");
50         delete mImage;
51         mImage = nullptr;
52     }
53 
54     updateTextureId();
55 }
56 
terminate()57 void AssetAtlas::terminate() {
58     if (mImage) {
59         delete mImage;
60         mImage = nullptr;
61         updateTextureId();
62     }
63 }
64 
65 
updateTextureId()66 void AssetAtlas::updateTextureId() {
67     mTexture->id = mImage ? mImage->getTexture() : 0;
68     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     }
74     for (size_t i = 0; i < mEntries.size(); i++) {
75         AssetAtlas::Entry* entry = mEntries.valueAt(i);
76         entry->texture->id = mTexture->id;
77     }
78 }
79 
80 ///////////////////////////////////////////////////////////////////////////////
81 // Entries
82 ///////////////////////////////////////////////////////////////////////////////
83 
getEntry(const SkBitmap * bitmap) const84 AssetAtlas::Entry* AssetAtlas::getEntry(const SkBitmap* bitmap) const {
85     ssize_t index = mEntries.indexOfKey(bitmap->pixelRef());
86     return index >= 0 ? mEntries.valueAt(index) : nullptr;
87 }
88 
getEntryTexture(const SkBitmap * bitmap) const89 Texture* AssetAtlas::getEntryTexture(const SkBitmap* bitmap) const {
90     ssize_t index = mEntries.indexOfKey(bitmap->pixelRef());
91     return index >= 0 ? mEntries.valueAt(index)->texture : nullptr;
92 }
93 
94 /**
95  * Delegates changes to wrapping and filtering to the base atlas texture
96  * instead of applying the changes to the virtual textures.
97  */
98 struct DelegateTexture: public Texture {
DelegateTextureandroid::uirenderer::DelegateTexture99     DelegateTexture(Caches& caches, Texture* delegate): Texture(caches), mDelegate(delegate) { }
100 
setWrapSTandroid::uirenderer::DelegateTexture101     virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
102             bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
103         mDelegate->setWrapST(wrapS, wrapT, bindTexture, force, renderTarget);
104     }
105 
setFilterMinMagandroid::uirenderer::DelegateTexture106     virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
107             bool force = false, GLenum renderTarget = GL_TEXTURE_2D) override {
108         mDelegate->setFilterMinMag(min, mag, bindTexture, force, renderTarget);
109     }
110 
111 private:
112     Texture* const mDelegate;
113 }; // struct DelegateTexture
114 
createEntries(Caches & caches,int64_t * map,int count)115 void AssetAtlas::createEntries(Caches& caches, int64_t* map, int count) {
116     const float width = float(mTexture->width);
117     const float height = float(mTexture->height);
118 
119     for (int i = 0; i < count; ) {
120         SkPixelRef* pixelRef = reinterpret_cast<SkPixelRef*>(map[i++]);
121         // NOTE: We're converting from 64 bit signed values to 32 bit
122         // signed values. This is guaranteed to be safe because the "x"
123         // and "y" coordinate values are guaranteed to be representable
124         // with 32 bits. The array is 64 bits wide so that it can carry
125         // pointers on 64 bit architectures.
126         const int x = static_cast<int>(map[i++]);
127         const int y = static_cast<int>(map[i++]);
128 
129         // Bitmaps should never be null, we're just extra paranoid
130         if (!pixelRef) continue;
131 
132         const UvMapper mapper(
133                 x / width, (x + pixelRef->info().width()) / width,
134                 y / height, (y + pixelRef->info().height()) / height);
135 
136         Texture* texture = new DelegateTexture(caches, mTexture);
137         texture->blend = !SkAlphaTypeIsOpaque(pixelRef->info().alphaType());
138         texture->width = pixelRef->info().width();
139         texture->height = pixelRef->info().height();
140 
141         Entry* entry = new Entry(pixelRef, texture, mapper, *this);
142         texture->uvMapper = &entry->uvMapper;
143 
144         mEntries.add(entry->pixelRef, entry);
145     }
146 }
147 
148 }; // namespace uirenderer
149 }; // namespace android
150