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