blob: 8d84a6bde1d73c5fde65de1f15895845c6abb8f8 [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):
Romain Guy9b1204b2012-09-04 15:22:57 -070053 mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL) {
Romain Guy9f5dab32012-09-04 12:55:44 -070054 }
55
56 static CacheBlock* insertBlock(CacheBlock* head, CacheBlock *newBlock);
57
58 static CacheBlock* removeBlock(CacheBlock* head, CacheBlock *blockToRemove);
59
60 void output() {
61 CacheBlock *currBlock = this;
62 while (currBlock) {
63 ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d",
64 currBlock, currBlock->mX, currBlock->mY, currBlock->mWidth, currBlock->mHeight);
65 currBlock = currBlock->mNext;
66 }
67 }
68};
69
70class CacheTexture {
71public:
72 CacheTexture(uint16_t width, uint16_t height) :
73 mTexture(NULL), mTextureId(0), mWidth(width), mHeight(height),
74 mLinearFiltering(false), mDirty(false), mNumGlyphs(0) {
75 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
76 mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
77 }
78
79 ~CacheTexture() {
80 if (mTexture) {
81 delete[] mTexture;
82 }
83 if (mTextureId) {
84 glDeleteTextures(1, &mTextureId);
85 }
86 reset();
87 }
88
89 void reset() {
90 // Delete existing cache blocks
91 while (mCacheBlocks != NULL) {
92 CacheBlock* tmpBlock = mCacheBlocks;
93 mCacheBlocks = mCacheBlocks->mNext;
94 delete tmpBlock;
95 }
96 mNumGlyphs = 0;
97 }
98
99 void init() {
100 // reset, then create a new remainder space to start again
101 reset();
102 mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE,
103 mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
104 }
105
106 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
107
108 uint8_t* mTexture;
109 GLuint mTextureId;
110 uint16_t mWidth;
111 uint16_t mHeight;
112 bool mLinearFiltering;
113 bool mDirty;
114 uint16_t mNumGlyphs;
115 CacheBlock* mCacheBlocks;
116};
117
118}; // namespace uirenderer
119}; // namespace android
120
121#endif // ANDROID_HWUI_CACHE_TEXTURE_H