blob: 5d3f959c2b981200aee36a3044e1245ffefe8436 [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
Romain Guy09087642013-04-04 12:27:54 -070020#include <GLES3/gl3.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070021
22#include <SkScalerContext.h>
23
24#include <utils/Log.h>
25
Victoria Lease1e546812013-06-25 14:25:17 -070026#include "../PixelBuffer.h"
Romain Guy661a87e2013-03-19 15:24:36 -070027#include "../Rect.h"
28#include "../Vertex.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070029
30namespace android {
31namespace uirenderer {
32
Romain Guy8aa195d2013-06-04 18:00:09 -070033class Caches;
Romain Guycf51a412013-04-08 19:40:31 -070034
Romain Guy9f5dab32012-09-04 12:55:44 -070035/**
36 * CacheBlock is a node in a linked list of current free space areas in a CacheTexture.
37 * Using CacheBlocks enables us to pack the cache from top to bottom as well as left to right.
38 * When we add a glyph to the cache, we see if it fits within one of the existing columns that
39 * have already been started (this is the case if the glyph fits vertically as well as
40 * horizontally, and if its width is sufficiently close to the column width to avoid
41 * sub-optimal packing of small glyphs into wide columns). If there is no column in which the
42 * glyph fits, we check the final node, which is the remaining space in the cache, creating
43 * a new column as appropriate.
44 *
45 * As columns fill up, we remove their CacheBlock from the list to avoid having to check
46 * small blocks in the future.
47 */
48struct CacheBlock {
49 uint16_t mX;
50 uint16_t mY;
51 uint16_t mWidth;
52 uint16_t mHeight;
53 CacheBlock* mNext;
54 CacheBlock* mPrev;
55
Chris Craike63f7c622013-10-17 10:30:55 -070056 CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height):
Chris Craike84a2082014-12-22 14:28:49 -080057 mX(x), mY(y), mWidth(width), mHeight(height), mNext(nullptr), mPrev(nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -070058 }
59
Romain Guye43f7852012-09-04 18:58:46 -070060 static CacheBlock* insertBlock(CacheBlock* head, CacheBlock* newBlock);
Romain Guye43f7852012-09-04 18:58:46 -070061 static CacheBlock* removeBlock(CacheBlock* head, CacheBlock* blockToRemove);
Romain Guy9f5dab32012-09-04 12:55:44 -070062
63 void output() {
Romain Guye43f7852012-09-04 18:58:46 -070064 CacheBlock* currBlock = this;
Romain Guy9f5dab32012-09-04 12:55:44 -070065 while (currBlock) {
66 ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d",
Romain Guy661a87e2013-03-19 15:24:36 -070067 currBlock, currBlock->mX, currBlock->mY,
68 currBlock->mWidth, currBlock->mHeight);
Romain Guy9f5dab32012-09-04 12:55:44 -070069 currBlock = currBlock->mNext;
70 }
71 }
72};
73
74class CacheTexture {
75public:
Victoria Lease1e546812013-06-25 14:25:17 -070076 CacheTexture(uint16_t width, uint16_t height, GLenum format, uint32_t maxQuadCount);
Romain Guy661a87e2013-03-19 15:24:36 -070077 ~CacheTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -070078
Romain Guy661a87e2013-03-19 15:24:36 -070079 void reset();
80 void init();
Romain Guy9f5dab32012-09-04 12:55:44 -070081
Romain Guy661a87e2013-03-19 15:24:36 -070082 void releaseMesh();
83 void releaseTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -070084
Romain Guy661a87e2013-03-19 15:24:36 -070085 void allocateTexture();
86 void allocateMesh();
Romain Guy80872462012-09-04 16:42:01 -070087
Romain Guycf51a412013-04-08 19:40:31 -070088 // Returns true if glPixelStorei(GL_UNPACK_ROW_LENGTH) must be reset
89 // This method will also call setDirty(false)
90 bool upload();
91
Romain Guye43f7852012-09-04 18:58:46 -070092 bool fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY);
Romain Guy9f5dab32012-09-04 12:55:44 -070093
Romain Guy80872462012-09-04 16:42:01 -070094 inline uint16_t getWidth() const {
95 return mWidth;
96 }
97
98 inline uint16_t getHeight() const {
99 return mHeight;
100 }
101
Victoria Lease1e546812013-06-25 14:25:17 -0700102 inline GLenum getFormat() const {
103 return mFormat;
104 }
105
106 inline uint32_t getOffset(uint16_t x, uint16_t y) const {
107 return (y * mWidth + x) * PixelBuffer::formatSize(mFormat);
108 }
109
Chet Haaseb92d8f72012-09-21 08:40:46 -0700110 inline const Rect* getDirtyRect() const {
111 return &mDirtyRect;
112 }
113
Romain Guycf51a412013-04-08 19:40:31 -0700114 inline PixelBuffer* getPixelBuffer() const {
Romain Guy80872462012-09-04 16:42:01 -0700115 return mTexture;
116 }
117
Romain Guy574cf602012-09-23 14:45:31 -0700118 GLuint getTextureId() {
119 allocateTexture();
Romain Guy80872462012-09-04 16:42:01 -0700120 return mTextureId;
121 }
122
123 inline bool isDirty() const {
124 return mDirty;
125 }
126
Romain Guy80872462012-09-04 16:42:01 -0700127 inline bool getLinearFiltering() const {
128 return mLinearFiltering;
129 }
130
131 /**
132 * This method assumes that the proper texture unit is active.
133 */
Romain Guycf51a412013-04-08 19:40:31 -0700134 void setLinearFiltering(bool linearFiltering, bool bind = true);
Romain Guy80872462012-09-04 16:42:01 -0700135
136 inline uint16_t getGlyphCount() const {
137 return mNumGlyphs;
138 }
139
Romain Guy661a87e2013-03-19 15:24:36 -0700140 TextureVertex* mesh() const {
141 return mMesh;
142 }
143
144 uint32_t meshElementCount() const {
145 return mCurrentQuad * 6;
146 }
147
148 uint16_t* indices() const {
Chris Craike84a2082014-12-22 14:28:49 -0800149 return (uint16_t*) nullptr;
Romain Guy661a87e2013-03-19 15:24:36 -0700150 }
151
152 void resetMesh() {
153 mCurrentQuad = 0;
154 }
155
156 inline void addQuad(float x1, float y1, float u1, float v1,
157 float x2, float y2, float u2, float v2,
158 float x3, float y3, float u3, float v3,
159 float x4, float y4, float u4, float v4) {
160 TextureVertex* mesh = mMesh + mCurrentQuad * 4;
Romain Guy661a87e2013-03-19 15:24:36 -0700161 TextureVertex::set(mesh++, x2, y2, u2, v2);
162 TextureVertex::set(mesh++, x3, y3, u3, v3);
Romain Guy31e08e92013-06-18 15:53:53 -0700163 TextureVertex::set(mesh++, x1, y1, u1, v1);
Romain Guy661a87e2013-03-19 15:24:36 -0700164 TextureVertex::set(mesh++, x4, y4, u4, v4);
165 mCurrentQuad++;
166 }
167
168 bool canDraw() const {
169 return mCurrentQuad > 0;
170 }
171
172 bool endOfMesh() const {
173 return mCurrentQuad == mMaxQuadCount;
174 }
175
Romain Guy80872462012-09-04 16:42:01 -0700176private:
Romain Guycf51a412013-04-08 19:40:31 -0700177 void setDirty(bool dirty);
178
179 PixelBuffer* mTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -0700180 GLuint mTextureId;
181 uint16_t mWidth;
182 uint16_t mHeight;
Victoria Lease1e546812013-06-25 14:25:17 -0700183 GLenum mFormat;
Romain Guy9f5dab32012-09-04 12:55:44 -0700184 bool mLinearFiltering;
185 bool mDirty;
186 uint16_t mNumGlyphs;
Romain Guy661a87e2013-03-19 15:24:36 -0700187 TextureVertex* mMesh;
188 uint32_t mCurrentQuad;
189 uint32_t mMaxQuadCount;
Romain Guy8aa195d2013-06-04 18:00:09 -0700190 Caches& mCaches;
Romain Guy9f5dab32012-09-04 12:55:44 -0700191 CacheBlock* mCacheBlocks;
Romain Guy318ae7b2013-09-24 18:44:54 -0700192 bool mHasUnpackRowLength;
Romain Guy8aa195d2013-06-04 18:00:09 -0700193 Rect mDirtyRect;
Romain Guy9f5dab32012-09-04 12:55:44 -0700194};
195
196}; // namespace uirenderer
197}; // namespace android
198
199#endif // ANDROID_HWUI_CACHE_TEXTURE_H