blob: 7840d740d3cbebc2365a7aae1426c46afbb24f33 [file] [log] [blame]
Romain Guy9f5dab32012-09-04 12:55:44 -07001/*
2 * Copyright (C) 2012 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_CACHE_TEXTURE_H
18#define ANDROID_HWUI_CACHE_TEXTURE_H
19
20#include <GLES2/gl2.h>
21
22#include <SkScalerContext.h>
23
24#include <utils/Log.h>
25
26#include "FontUtil.h"
27
28namespace android {
29namespace uirenderer {
30
31/**
32 * CacheBlock is a node in a linked list of current free space areas in a CacheTexture.
33 * Using CacheBlocks enables us to pack the cache from top to bottom as well as left to right.
34 * When we add a glyph to the cache, we see if it fits within one of the existing columns that
35 * have already been started (this is the case if the glyph fits vertically as well as
36 * horizontally, and if its width is sufficiently close to the column width to avoid
37 * sub-optimal packing of small glyphs into wide columns). If there is no column in which the
38 * glyph fits, we check the final node, which is the remaining space in the cache, creating
39 * a new column as appropriate.
40 *
41 * As columns fill up, we remove their CacheBlock from the list to avoid having to check
42 * small blocks in the future.
43 */
44struct CacheBlock {
45 uint16_t mX;
46 uint16_t mY;
47 uint16_t mWidth;
48 uint16_t mHeight;
49 CacheBlock* mNext;
50 CacheBlock* mPrev;
51
52 CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool empty = false):
53 mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL)
54 {
55 }
56
57 static CacheBlock* insertBlock(CacheBlock* head, CacheBlock *newBlock);
58
59 static CacheBlock* removeBlock(CacheBlock* head, CacheBlock *blockToRemove);
60
61 void output() {
62 CacheBlock *currBlock = this;
63 while (currBlock) {
64 ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d",
65 currBlock, currBlock->mX, currBlock->mY, currBlock->mWidth, currBlock->mHeight);
66 currBlock = currBlock->mNext;
67 }
68 }
69};
70
71class CacheTexture {
72public:
73 CacheTexture(uint16_t width, uint16_t height) :
74 mTexture(NULL), mTextureId(0), mWidth(width), mHeight(height),
75 mLinearFiltering(false), mDirty(false), mNumGlyphs(0) {
76 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
77 mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
78 }
79
80 ~CacheTexture() {
81 if (mTexture) {
82 delete[] mTexture;
83 }
84 if (mTextureId) {
85 glDeleteTextures(1, &mTextureId);
86 }
87 reset();
88 }
89
90 void reset() {
91 // Delete existing cache blocks
92 while (mCacheBlocks != NULL) {
93 CacheBlock* tmpBlock = mCacheBlocks;
94 mCacheBlocks = mCacheBlocks->mNext;
95 delete tmpBlock;
96 }
97 mNumGlyphs = 0;
98 }
99
100 void init() {
101 // reset, then create a new remainder space to start again
102 reset();
103 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
104 mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
105 }
106
107 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
108
109 uint8_t* mTexture;
110 GLuint mTextureId;
111 uint16_t mWidth;
112 uint16_t mHeight;
113 bool mLinearFiltering;
114 bool mDirty;
115 uint16_t mNumGlyphs;
116 CacheBlock* mCacheBlocks;
117};
118
119}; // namespace uirenderer
120}; // namespace android
121
122#endif // ANDROID_HWUI_CACHE_TEXTURE_H