blob: 654378eeb47eff6b643846784eea9546f19a55b3 [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
Chris Craike2bb3802015-03-13 15:07:52 -070020#include "PixelBuffer.h"
21#include "Rect.h"
22#include "Texture.h"
23#include "Vertex.h"
24
Romain Guy09087642013-04-04 12:27:54 -070025#include <GLES3/gl3.h>
Leon Scroggins IIIc46813d2016-02-16 10:20:11 -050026#include <SkGlyph.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070027#include <utils/Log.h>
28
Romain Guy9f5dab32012-09-04 12:55:44 -070029namespace android {
30namespace uirenderer {
31
Romain Guy8aa195d2013-06-04 18:00:09 -070032class Caches;
Romain Guycf51a412013-04-08 19:40:31 -070033
Romain Guy9f5dab32012-09-04 12:55:44 -070034/**
35 * CacheBlock is a node in a linked list of current free space areas in a CacheTexture.
36 * Using CacheBlocks enables us to pack the cache from top to bottom as well as left to right.
37 * When we add a glyph to the cache, we see if it fits within one of the existing columns that
38 * have already been started (this is the case if the glyph fits vertically as well as
39 * horizontally, and if its width is sufficiently close to the column width to avoid
40 * sub-optimal packing of small glyphs into wide columns). If there is no column in which the
41 * glyph fits, we check the final node, which is the remaining space in the cache, creating
42 * a new column as appropriate.
43 *
44 * As columns fill up, we remove their CacheBlock from the list to avoid having to check
45 * small blocks in the future.
46 */
47struct CacheBlock {
48 uint16_t mX;
49 uint16_t mY;
50 uint16_t mWidth;
51 uint16_t mHeight;
52 CacheBlock* mNext;
53 CacheBlock* mPrev;
54
John Reck1bcacfd2017-11-03 10:12:19 -070055 CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
56 : mX(x), mY(y), mWidth(width), mHeight(height), mNext(nullptr), mPrev(nullptr) {}
Romain Guy9f5dab32012-09-04 12:55:44 -070057
Romain Guye43f7852012-09-04 18:58:46 -070058 static CacheBlock* insertBlock(CacheBlock* head, CacheBlock* newBlock);
Romain Guye43f7852012-09-04 18:58:46 -070059 static CacheBlock* removeBlock(CacheBlock* head, CacheBlock* blockToRemove);
Romain Guy9f5dab32012-09-04 12:55:44 -070060
61 void output() {
Romain Guye43f7852012-09-04 18:58:46 -070062 CacheBlock* currBlock = this;
Romain Guy9f5dab32012-09-04 12:55:44 -070063 while (currBlock) {
John Reck1bcacfd2017-11-03 10:12:19 -070064 ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d", currBlock, currBlock->mX,
65 currBlock->mY, currBlock->mWidth, currBlock->mHeight);
Romain Guy9f5dab32012-09-04 12:55:44 -070066 currBlock = currBlock->mNext;
67 }
68 }
69};
70
71class CacheTexture {
72public:
Victoria Lease1e546812013-06-25 14:25:17 -070073 CacheTexture(uint16_t width, uint16_t height, GLenum format, uint32_t maxQuadCount);
Romain Guy661a87e2013-03-19 15:24:36 -070074 ~CacheTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -070075
Romain Guy661a87e2013-03-19 15:24:36 -070076 void reset();
77 void init();
Romain Guy9f5dab32012-09-04 12:55:44 -070078
Romain Guy661a87e2013-03-19 15:24:36 -070079 void releaseMesh();
Chris Craike2bb3802015-03-13 15:07:52 -070080 void releasePixelBuffer();
Romain Guy9f5dab32012-09-04 12:55:44 -070081
Chris Craike2bb3802015-03-13 15:07:52 -070082 void allocatePixelBuffer();
Romain Guy661a87e2013-03-19 15:24:36 -070083 void allocateMesh();
Romain Guy80872462012-09-04 16:42:01 -070084
Romain Guycf51a412013-04-08 19:40:31 -070085 // Returns true if glPixelStorei(GL_UNPACK_ROW_LENGTH) must be reset
86 // This method will also call setDirty(false)
87 bool upload();
88
Romain Guye43f7852012-09-04 18:58:46 -070089 bool fitBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY);
Romain Guy9f5dab32012-09-04 12:55:44 -070090
John Reck1bcacfd2017-11-03 10:12:19 -070091 inline uint16_t getWidth() const { return mWidth; }
Romain Guy80872462012-09-04 16:42:01 -070092
John Reck1bcacfd2017-11-03 10:12:19 -070093 inline uint16_t getHeight() const { return mHeight; }
Romain Guy80872462012-09-04 16:42:01 -070094
John Reck1bcacfd2017-11-03 10:12:19 -070095 inline GLenum getFormat() const { return mFormat; }
Victoria Lease1e546812013-06-25 14:25:17 -070096
97 inline uint32_t getOffset(uint16_t x, uint16_t y) const {
Chris Craike2bb3802015-03-13 15:07:52 -070098 return (y * getWidth() + x) * PixelBuffer::formatSize(mFormat);
Victoria Lease1e546812013-06-25 14:25:17 -070099 }
100
John Reck1bcacfd2017-11-03 10:12:19 -0700101 inline const Rect* getDirtyRect() const { return &mDirtyRect; }
Chet Haaseb92d8f72012-09-21 08:40:46 -0700102
John Reck1bcacfd2017-11-03 10:12:19 -0700103 inline PixelBuffer* getPixelBuffer() const { return mPixelBuffer; }
Chris Craike2bb3802015-03-13 15:07:52 -0700104
105 Texture& getTexture() {
106 allocatePixelBuffer();
Romain Guy80872462012-09-04 16:42:01 -0700107 return mTexture;
108 }
109
Romain Guy574cf602012-09-23 14:45:31 -0700110 GLuint getTextureId() {
Chris Craike2bb3802015-03-13 15:07:52 -0700111 allocatePixelBuffer();
John Reck38e0c322015-11-10 12:19:17 -0800112 return mTexture.id();
Romain Guy80872462012-09-04 16:42:01 -0700113 }
114
John Reck1bcacfd2017-11-03 10:12:19 -0700115 inline bool isDirty() const { return mDirty; }
Romain Guy80872462012-09-04 16:42:01 -0700116
John Reck1bcacfd2017-11-03 10:12:19 -0700117 inline bool getLinearFiltering() const { return mLinearFiltering; }
Romain Guy80872462012-09-04 16:42:01 -0700118
119 /**
120 * This method assumes that the proper texture unit is active.
121 */
Chris Craike2bb3802015-03-13 15:07:52 -0700122 void setLinearFiltering(bool linearFiltering);
Romain Guy80872462012-09-04 16:42:01 -0700123
John Reck1bcacfd2017-11-03 10:12:19 -0700124 inline uint16_t getGlyphCount() const { return mNumGlyphs; }
Romain Guy80872462012-09-04 16:42:01 -0700125
John Reck1bcacfd2017-11-03 10:12:19 -0700126 TextureVertex* mesh() const { return mMesh; }
Romain Guy661a87e2013-03-19 15:24:36 -0700127
John Reck1bcacfd2017-11-03 10:12:19 -0700128 uint32_t meshElementCount() const { return mCurrentQuad * 6; }
Romain Guy661a87e2013-03-19 15:24:36 -0700129
John Reck1bcacfd2017-11-03 10:12:19 -0700130 uint16_t* indices() const { return (uint16_t*)nullptr; }
Romain Guy661a87e2013-03-19 15:24:36 -0700131
John Reck1bcacfd2017-11-03 10:12:19 -0700132 void resetMesh() { mCurrentQuad = 0; }
Romain Guy661a87e2013-03-19 15:24:36 -0700133
John Reck1bcacfd2017-11-03 10:12:19 -0700134 inline void addQuad(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
135 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
136 float u4, float v4) {
Romain Guy661a87e2013-03-19 15:24:36 -0700137 TextureVertex* mesh = mMesh + mCurrentQuad * 4;
Romain Guy661a87e2013-03-19 15:24:36 -0700138 TextureVertex::set(mesh++, x2, y2, u2, v2);
139 TextureVertex::set(mesh++, x3, y3, u3, v3);
Romain Guy31e08e92013-06-18 15:53:53 -0700140 TextureVertex::set(mesh++, x1, y1, u1, v1);
Romain Guy661a87e2013-03-19 15:24:36 -0700141 TextureVertex::set(mesh++, x4, y4, u4, v4);
142 mCurrentQuad++;
143 }
144
John Reck1bcacfd2017-11-03 10:12:19 -0700145 bool canDraw() const { return mCurrentQuad > 0; }
Romain Guy661a87e2013-03-19 15:24:36 -0700146
John Reck1bcacfd2017-11-03 10:12:19 -0700147 bool endOfMesh() const { return mCurrentQuad == mMaxQuadCount; }
Romain Guy661a87e2013-03-19 15:24:36 -0700148
sergeyvbaf29e72016-09-08 11:09:34 -0700149 uint32_t calculateFreeMemory() const;
150
Romain Guy80872462012-09-04 16:42:01 -0700151private:
Romain Guycf51a412013-04-08 19:40:31 -0700152 void setDirty(bool dirty);
153
Chris Craike2bb3802015-03-13 15:07:52 -0700154 PixelBuffer* mPixelBuffer = nullptr;
155 Texture mTexture;
John Reck38e0c322015-11-10 12:19:17 -0800156 uint32_t mWidth, mHeight;
Victoria Lease1e546812013-06-25 14:25:17 -0700157 GLenum mFormat;
Chris Craike2bb3802015-03-13 15:07:52 -0700158 bool mLinearFiltering = false;
159 bool mDirty = false;
160 uint16_t mNumGlyphs = 0;
161 TextureVertex* mMesh = nullptr;
162 uint32_t mCurrentQuad = 0;
Romain Guy661a87e2013-03-19 15:24:36 -0700163 uint32_t mMaxQuadCount;
Romain Guy8aa195d2013-06-04 18:00:09 -0700164 Caches& mCaches;
Romain Guy9f5dab32012-09-04 12:55:44 -0700165 CacheBlock* mCacheBlocks;
Romain Guy318ae7b2013-09-24 18:44:54 -0700166 bool mHasUnpackRowLength;
Romain Guy8aa195d2013-06-04 18:00:09 -0700167 Rect mDirtyRect;
Romain Guy9f5dab32012-09-04 12:55:44 -0700168};
169
John Reck1bcacfd2017-11-03 10:12:19 -0700170}; // namespace uirenderer
171}; // namespace android
Romain Guy9f5dab32012-09-04 12:55:44 -0700172
John Reck1bcacfd2017-11-03 10:12:19 -0700173#endif // ANDROID_HWUI_CACHE_TEXTURE_H