2012-09-04 12:55:44 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_HWUI_CACHE_TEXTURE_H
|
|
|
|
#define ANDROID_HWUI_CACHE_TEXTURE_H
|
|
|
|
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
|
|
|
|
#include <SkScalerContext.h>
|
|
|
|
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
#include "FontUtil.h"
|
2013-03-19 15:24:36 -07:00
|
|
|
#include "../Rect.h"
|
|
|
|
#include "../Vertex.h"
|
2012-09-04 12:55:44 -07:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CacheBlock is a node in a linked list of current free space areas in a CacheTexture.
|
|
|
|
* Using CacheBlocks enables us to pack the cache from top to bottom as well as left to right.
|
|
|
|
* When we add a glyph to the cache, we see if it fits within one of the existing columns that
|
|
|
|
* have already been started (this is the case if the glyph fits vertically as well as
|
|
|
|
* horizontally, and if its width is sufficiently close to the column width to avoid
|
|
|
|
* sub-optimal packing of small glyphs into wide columns). If there is no column in which the
|
|
|
|
* glyph fits, we check the final node, which is the remaining space in the cache, creating
|
|
|
|
* a new column as appropriate.
|
|
|
|
*
|
|
|
|
* As columns fill up, we remove their CacheBlock from the list to avoid having to check
|
|
|
|
* small blocks in the future.
|
|
|
|
*/
|
|
|
|
struct CacheBlock {
|
|
|
|
uint16_t mX;
|
|
|
|
uint16_t mY;
|
|
|
|
uint16_t mWidth;
|
|
|
|
uint16_t mHeight;
|
|
|
|
CacheBlock* mNext;
|
|
|
|
CacheBlock* mPrev;
|
|
|
|
|
|
|
|
CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool empty = false):
|
2012-09-04 15:22:57 -07:00
|
|
|
mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL) {
|
2012-09-04 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 18:58:46 -07:00
|
|
|
static CacheBlock* insertBlock(CacheBlock* head, CacheBlock* newBlock);
|
|
|
|
static CacheBlock* removeBlock(CacheBlock* head, CacheBlock* blockToRemove);
|
2012-09-04 12:55:44 -07:00
|
|
|
|
|
|
|
void output() {
|
2012-09-04 18:58:46 -07:00
|
|
|
CacheBlock* currBlock = this;
|
2012-09-04 12:55:44 -07:00
|
|
|
while (currBlock) {
|
|
|
|
ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d",
|
2013-03-19 15:24:36 -07:00
|
|
|
currBlock, currBlock->mX, currBlock->mY,
|
|
|
|
currBlock->mWidth, currBlock->mHeight);
|
2012-09-04 12:55:44 -07:00
|
|
|
currBlock = currBlock->mNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CacheTexture {
|
|
|
|
public:
|
2013-03-19 15:24:36 -07:00
|
|
|
CacheTexture(uint16_t width, uint16_t height, uint32_t maxQuadCount);
|
|
|
|
~CacheTexture();
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2013-03-19 15:24:36 -07:00
|
|
|
void reset();
|
|
|
|
void init();
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2013-03-19 15:24:36 -07:00
|
|
|
void releaseMesh();
|
|
|
|
void releaseTexture();
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2013-03-19 15:24:36 -07:00
|
|
|
void allocateTexture();
|
|
|
|
void allocateMesh();
|
2012-09-04 16:42:01 -07:00
|
|
|
|
2012-09-04 18:58:46 -07:00
|
|
|
bool fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY);
|
2012-09-04 12:55:44 -07:00
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
inline uint16_t getWidth() const {
|
|
|
|
return mWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t getHeight() const {
|
|
|
|
return mHeight;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:40:46 -07:00
|
|
|
inline const Rect* getDirtyRect() const {
|
|
|
|
return &mDirtyRect;
|
|
|
|
}
|
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
inline uint8_t* getTexture() const {
|
|
|
|
return mTexture;
|
|
|
|
}
|
|
|
|
|
2012-09-23 14:45:31 -07:00
|
|
|
GLuint getTextureId() {
|
|
|
|
allocateTexture();
|
2012-09-04 16:42:01 -07:00
|
|
|
return mTextureId;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isDirty() const {
|
|
|
|
return mDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void setDirty(bool dirty) {
|
|
|
|
mDirty = dirty;
|
2012-09-21 08:40:46 -07:00
|
|
|
if (!dirty) {
|
|
|
|
mDirtyRect.setEmpty();
|
|
|
|
}
|
2012-09-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool getLinearFiltering() const {
|
|
|
|
return mLinearFiltering;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method assumes that the proper texture unit is active.
|
|
|
|
*/
|
|
|
|
void setLinearFiltering(bool linearFiltering, bool bind = true) {
|
|
|
|
if (linearFiltering != mLinearFiltering) {
|
|
|
|
mLinearFiltering = linearFiltering;
|
|
|
|
|
|
|
|
const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST;
|
|
|
|
if (bind) glBindTexture(GL_TEXTURE_2D, getTextureId());
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t getGlyphCount() const {
|
|
|
|
return mNumGlyphs;
|
|
|
|
}
|
|
|
|
|
2013-03-19 15:24:36 -07:00
|
|
|
TextureVertex* mesh() const {
|
|
|
|
return mMesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t meshElementCount() const {
|
|
|
|
return mCurrentQuad * 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t* indices() const {
|
|
|
|
return (uint16_t*) 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetMesh() {
|
|
|
|
mCurrentQuad = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void addQuad(float x1, float y1, float u1, float v1,
|
|
|
|
float x2, float y2, float u2, float v2,
|
|
|
|
float x3, float y3, float u3, float v3,
|
|
|
|
float x4, float y4, float u4, float v4) {
|
|
|
|
TextureVertex* mesh = mMesh + mCurrentQuad * 4;
|
|
|
|
TextureVertex::set(mesh++, x1, y1, u1, v1);
|
|
|
|
TextureVertex::set(mesh++, x2, y2, u2, v2);
|
|
|
|
TextureVertex::set(mesh++, x3, y3, u3, v3);
|
|
|
|
TextureVertex::set(mesh++, x4, y4, u4, v4);
|
|
|
|
mCurrentQuad++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool canDraw() const {
|
|
|
|
return mCurrentQuad > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool endOfMesh() const {
|
|
|
|
return mCurrentQuad == mMaxQuadCount;
|
|
|
|
}
|
|
|
|
|
2012-09-04 16:42:01 -07:00
|
|
|
private:
|
2012-09-04 12:55:44 -07:00
|
|
|
uint8_t* mTexture;
|
|
|
|
GLuint mTextureId;
|
|
|
|
uint16_t mWidth;
|
|
|
|
uint16_t mHeight;
|
|
|
|
bool mLinearFiltering;
|
|
|
|
bool mDirty;
|
|
|
|
uint16_t mNumGlyphs;
|
2013-03-19 15:24:36 -07:00
|
|
|
TextureVertex* mMesh;
|
|
|
|
uint32_t mCurrentQuad;
|
|
|
|
uint32_t mMaxQuadCount;
|
2012-09-04 12:55:44 -07:00
|
|
|
CacheBlock* mCacheBlocks;
|
2012-09-21 08:40:46 -07:00
|
|
|
Rect mDirtyRect;
|
2012-09-04 12:55:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif // ANDROID_HWUI_CACHE_TEXTURE_H
|