DisplayListRenderer.h revision 4aa90573bbf86db0d33a3a790c5dbd0d93b95cfe
1/* 2 * Copyright (C) 2010 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_UI_DISPLAY_LIST_RENDERER_H 18#define ANDROID_UI_DISPLAY_LIST_RENDERER_H 19 20#include <SkChunkAlloc.h> 21#include <SkFlattenable.h> 22#include <SkMatrix.h> 23#include <SkPaint.h> 24#include <SkPath.h> 25#include <SkPictureFlat.h> 26#include <SkRefCnt.h> 27#include <SkTDArray.h> 28#include <SkTSearch.h> 29 30#include "OpenGLRenderer.h" 31 32namespace android { 33namespace uirenderer { 34 35/////////////////////////////////////////////////////////////////////////////// 36// Defines 37/////////////////////////////////////////////////////////////////////////////// 38 39#define MIN_WRITER_SIZE 16384 40#define HEAP_BLOCK_SIZE 4096 41 42/////////////////////////////////////////////////////////////////////////////// 43// Helpers 44/////////////////////////////////////////////////////////////////////////////// 45 46class PathHeap: public SkRefCnt { 47public: 48 PathHeap(): mHeap(64 * sizeof(SkPath)) { 49 }; 50 51 PathHeap(SkFlattenableReadBuffer& buffer): mHeap(64 * sizeof(SkPath)) { 52 int count = buffer.readS32(); 53 54 mPaths.setCount(count); 55 SkPath** ptr = mPaths.begin(); 56 SkPath* p = (SkPath*) mHeap.allocThrow(count * sizeof(SkPath)); 57 58 for (int i = 0; i < count; i++) { 59 new (p) SkPath; 60 p->unflatten(buffer); 61 *ptr++ = p; 62 p++; 63 } 64 } 65 66 ~PathHeap() { 67 SkPath** iter = mPaths.begin(); 68 SkPath** stop = mPaths.end(); 69 while (iter < stop) { 70 (*iter)->~SkPath(); 71 iter++; 72 } 73 } 74 75 int append(const SkPath& path) { 76 SkPath* p = (SkPath*) mHeap.allocThrow(sizeof(SkPath)); 77 new (p) SkPath(path); 78 *mPaths.append() = p; 79 return mPaths.count(); 80 } 81 82 int count() const { return mPaths.count(); } 83 84 const SkPath& operator[](int index) const { 85 return *mPaths[index]; 86 } 87 88 void flatten(SkFlattenableWriteBuffer& buffer) const { 89 int count = mPaths.count(); 90 91 buffer.write32(count); 92 SkPath** iter = mPaths.begin(); 93 SkPath** stop = mPaths.end(); 94 while (iter < stop) { 95 (*iter)->flatten(buffer); 96 iter++; 97 } 98 } 99 100private: 101 SkChunkAlloc mHeap; 102 SkTDArray<SkPath*> mPaths; 103}; 104 105/////////////////////////////////////////////////////////////////////////////// 106// Renderer 107/////////////////////////////////////////////////////////////////////////////// 108 109/** 110 * Records drawing commands in a display list for latter playback. 111 */ 112class DisplayListRenderer: public OpenGLRenderer { 113public: 114 DisplayListRenderer(); 115 ~DisplayListRenderer(); 116 117 enum Op { 118 AcquireContext, 119 ReleaseContext, 120 Save, 121 Restore, 122 RestoreToCount, 123 SaveLayer, 124 SaveLayerAlpha, 125 Translate, 126 Rotate, 127 Scale, 128 SetMatrix, 129 ConcatMatrix, 130 ClipRect, 131 DrawBitmap, 132 DrawBitmapMatrix, 133 DrawBitmapRect, 134 DrawPatch, 135 DrawColor, 136 DrawRect, 137 DrawPath, 138 DrawLines, 139 DrawText, 140 ResetShader, 141 SetupShader, 142 ResetColorFilter, 143 SetupColorFilter, 144 ResetShadow, 145 SetupShadow 146 }; 147 148 void acquireContext(); 149 void releaseContext(); 150 151 int save(int flags); 152 void restore(); 153 void restoreToCount(int saveCount); 154 155 int saveLayer(float left, float top, float right, float bottom, 156 const SkPaint* p, int flags); 157 158 void translate(float dx, float dy); 159 void rotate(float degrees); 160 void scale(float sx, float sy); 161 162 void setMatrix(SkMatrix* matrix); 163 void concatMatrix(SkMatrix* matrix); 164 165 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); 166 167 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint); 168 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint); 169 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, 170 float srcRight, float srcBottom, float dstLeft, float dstTop, 171 float dstRight, float dstBottom, const SkPaint* paint); 172 void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 173 uint32_t width, uint32_t height, float left, float top, float right, float bottom, 174 const SkPaint* paint); 175 void drawColor(int color, SkXfermode::Mode mode); 176 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint); 177 void drawPath(SkPath* path, SkPaint* paint); 178 void drawLines(float* points, int count, const SkPaint* paint); 179 void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint); 180 181 void resetShader(); 182 void setupShader(SkiaShader* shader); 183 184 void resetColorFilter(); 185 void setupColorFilter(SkiaColorFilter* filter); 186 187 void resetShadow(); 188 void setupShadow(float radius, float dx, float dy, int color); 189 190 void reset(); 191 192private: 193 inline void addOp(Op drawOp) { 194 mWriter.writeInt(drawOp); 195 } 196 197 inline void addInt(int value) { 198 mWriter.writeInt(value); 199 } 200 201 void addInts(const int32_t* values, uint32_t count) { 202 for (uint32_t i = 0; i < count; i++) { 203 mWriter.writeInt(values[i]); 204 } 205 } 206 207 inline void addFloat(float value) { 208 mWriter.writeScalar(value); 209 } 210 211 void addFloats(const float* values, int count) { 212 for (int i = 0; i < count; i++) { 213 mWriter.writeScalar(values[i]); 214 } 215 } 216 217 inline void addPoint(float x, float y) { 218 mWriter.writeScalar(x); 219 mWriter.writeScalar(y); 220 } 221 222 inline void addBounds(float left, float top, float right, float bottom) { 223 mWriter.writeScalar(left); 224 mWriter.writeScalar(top); 225 mWriter.writeScalar(right); 226 mWriter.writeScalar(bottom); 227 } 228 229 inline void addText(const void* text, size_t byteLength) { 230 mWriter.writeInt(byteLength); 231 mWriter.writePad(text, byteLength); 232 } 233 234 inline void addPath(const SkPath* path) { 235 if (mPathHeap == NULL) { 236 mPathHeap = new PathHeap(); 237 } 238 addInt(mPathHeap->append(*path)); 239 } 240 241 int find(SkTDArray<const SkFlatPaint*>& paints, const SkPaint* paint); 242 243 inline void addPaint(const SkPaint* paint) { 244 addInt(find(mPaints, paint)); 245 } 246 247 int find(SkTDArray<const SkFlatMatrix*>& matrices, const SkMatrix* matrix); 248 249 inline void addMatrix(const SkMatrix* matrix) { 250 addInt(find(mMatrices, matrix)); 251 } 252 253 int find(SkTDArray<const SkFlatBitmap*>& bitmaps, const SkBitmap& bitmap); 254 255 inline void addBitmap(const SkBitmap* bitmap) { 256 addInt(find(mBitmaps, *bitmap)); 257 } 258 259 SkChunkAlloc mHeap; 260 261 int mBitmapIndex; 262 SkTDArray<const SkFlatBitmap*> mBitmaps; 263 264 int mMatrixIndex; 265 SkTDArray<const SkFlatMatrix*> mMatrices; 266 267 int mPaintIndex; 268 SkTDArray<const SkFlatPaint*> mPaints; 269 270 PathHeap* mPathHeap; 271 SkWriter32 mWriter; 272 273 SkRefCntRecorder mRCRecorder; 274 SkRefCntRecorder mTFRecorder; 275 276}; // class DisplayListRenderer 277 278}; // namespace uirenderer 279}; // namespace android 280 281#endif // ANDROID_UI_DISPLAY_LIST_RENDERER_H 282