Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2010 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 #ifndef ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
     18 #define ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
     19 
     20 #include <GLES2/gl2.h>
     21 
     22 #include <SkPaint.h>
     23 
     24 #include <utils/LruCache.h>
     25 #include <utils/String16.h>
     26 
     27 #include "FontRenderer.h"
     28 #include "Texture.h"
     29 
     30 namespace android {
     31 namespace uirenderer {
     32 
     33 struct ShadowText {
     34     ShadowText(): len(0), radius(0.0f), textSize(0.0f), typeface(NULL),
     35             flags(0), italicStyle(0.0f), scaleX(0), text(NULL), positions(NULL) {
     36     }
     37 
     38     // len is the number of bytes in text
     39     ShadowText(SkPaint* paint, float radius, uint32_t len, const char* srcText,
     40             const float* positions):
     41             len(len), radius(radius), positions(positions) {
     42         // TODO: Propagate this through the API, we should not cast here
     43         text = (const char16_t*) srcText;
     44 
     45         textSize = paint->getTextSize();
     46         typeface = paint->getTypeface();
     47 
     48         flags = 0;
     49         if (paint->isFakeBoldText()) {
     50             flags |= Font::kFakeBold;
     51         }
     52 
     53         italicStyle = paint->getTextSkewX();
     54         scaleX = paint->getTextScaleX();
     55     }
     56 
     57     ~ShadowText() {
     58     }
     59 
     60     hash_t hash() const;
     61 
     62     static int compare(const ShadowText& lhs, const ShadowText& rhs);
     63 
     64     bool operator==(const ShadowText& other) const {
     65         return compare(*this, other) == 0;
     66     }
     67 
     68     bool operator!=(const ShadowText& other) const {
     69         return compare(*this, other) != 0;
     70     }
     71 
     72     void copyTextLocally() {
     73         uint32_t charCount = len / sizeof(char16_t);
     74         str.setTo((const char16_t*) text, charCount);
     75         text = str.string();
     76         if (positions != NULL) {
     77             positionsCopy.clear();
     78             positionsCopy.appendArray(positions, charCount * 2);
     79             positions = positionsCopy.array();
     80         }
     81     }
     82 
     83     uint32_t len;
     84     float radius;
     85     float textSize;
     86     SkTypeface* typeface;
     87     uint32_t flags;
     88     float italicStyle;
     89     float scaleX;
     90     const char16_t* text;
     91     const float* positions;
     92 
     93     // Not directly used to compute the cache key
     94     String16 str;
     95     Vector<float> positionsCopy;
     96 
     97 }; // struct ShadowText
     98 
     99 // Caching support
    100 
    101 inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
    102     return ShadowText::compare(lhs, rhs) < 0;
    103 }
    104 
    105 inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
    106     return ShadowText::compare(lhs, rhs);
    107 }
    108 
    109 inline hash_t hash_type(const ShadowText& entry) {
    110     return entry.hash();
    111 }
    112 
    113 /**
    114  * Alpha texture used to represent a shadow.
    115  */
    116 struct ShadowTexture: public Texture {
    117     ShadowTexture(): Texture() {
    118     }
    119 
    120     float left;
    121     float top;
    122 }; // struct ShadowTexture
    123 
    124 class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
    125 public:
    126     TextDropShadowCache();
    127     TextDropShadowCache(uint32_t maxByteSize);
    128     ~TextDropShadowCache();
    129 
    130     /**
    131      * Used as a callback when an entry is removed from the cache.
    132      * Do not invoke directly.
    133      */
    134     void operator()(ShadowText& text, ShadowTexture*& texture);
    135 
    136     ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
    137             int numGlyphs, float radius, const float* positions);
    138 
    139     /**
    140      * Clears the cache. This causes all textures to be deleted.
    141      */
    142     void clear();
    143 
    144     void setFontRenderer(FontRenderer& fontRenderer) {
    145         mRenderer = &fontRenderer;
    146     }
    147 
    148     /**
    149      * Sets the maximum size of the cache in bytes.
    150      */
    151     void setMaxSize(uint32_t maxSize);
    152     /**
    153      * Returns the maximum size of the cache in bytes.
    154      */
    155     uint32_t getMaxSize();
    156     /**
    157      * Returns the current size of the cache in bytes.
    158      */
    159     uint32_t getSize();
    160 
    161 private:
    162     void init();
    163 
    164     LruCache<ShadowText, ShadowTexture*> mCache;
    165 
    166     uint32_t mSize;
    167     uint32_t mMaxSize;
    168     FontRenderer* mRenderer;
    169     bool mDebugEnabled;
    170 }; // class TextDropShadowCache
    171 
    172 }; // namespace uirenderer
    173 }; // namespace android
    174 
    175 #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
    176