DisplayListRenderer.h revision 7975fb6d12cb1eb96b75e3a563627cd4c4081bd6
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(); 49 PathHeap(SkFlattenableReadBuffer& buffer); 50 ~PathHeap(); 51 52 int append(const SkPath& path); 53 54 int count() const { return mPaths.count(); } 55 56 SkPath& operator[](int index) const { 57 return *mPaths[index]; 58 } 59 60 void flatten(SkFlattenableWriteBuffer& buffer) const; 61 62private: 63 SkChunkAlloc mHeap; 64 SkTDArray<SkPath*> mPaths; 65}; 66 67/////////////////////////////////////////////////////////////////////////////// 68// Display list 69/////////////////////////////////////////////////////////////////////////////// 70 71class DisplayListRenderer; 72 73/** 74 * Replays recorded drawing commands. 75 */ 76class DisplayList { 77public: 78 DisplayList(const DisplayListRenderer& recorder); 79 ~DisplayList(); 80 81 enum Op { 82 AcquireContext, 83 ReleaseContext, 84 Save, 85 Restore, 86 RestoreToCount, 87 SaveLayer, 88 Translate, 89 Rotate, 90 Scale, 91 SetMatrix, 92 ConcatMatrix, 93 ClipRect, 94 DrawBitmap, 95 DrawBitmapMatrix, 96 DrawBitmapRect, 97 DrawPatch, 98 DrawColor, 99 DrawRect, 100 DrawPath, 101 DrawLines, 102 DrawText, 103 ResetShader, 104 SetupShader, 105 ResetColorFilter, 106 SetupColorFilter, 107 ResetShadow, 108 SetupShadow 109 }; 110 111 void replay(OpenGLRenderer& renderer); 112 113private: 114 void init(); 115 116 class TextContainer { 117 public: 118 size_t length() const { 119 return mByteLength; 120 } 121 122 const char* text() const { 123 return (const char*) mText; 124 } 125 126 size_t mByteLength; 127 const char* mText; 128 }; 129 130 SkBitmap* getBitmap() { 131 int index = getInt(); 132 return &mBitmaps[index - 1]; 133 } 134 135 inline int getIndex() { 136 return mReader.readInt(); 137 } 138 139 inline int getInt() { 140 return mReader.readInt(); 141 } 142 143 SkMatrix* getMatrix() { 144 int index = getInt(); 145 if (index == 0) { 146 return NULL; 147 } 148 return &mMatrices[index - 1]; 149 } 150 151 SkPath* getPath() { 152 return &(*mPathHeap)[getInt() - 1]; 153 } 154 155 SkPaint* getPaint() { 156 int index = getInt(); 157 if (index == 0) { 158 return NULL; 159 } 160 return &mPaints[index - 1]; 161 } 162 163 inline float getFloat() { 164 return mReader.readScalar(); 165 } 166 167 int32_t* getInts(uint32_t& count) { 168 count = getInt(); 169 return (int32_t*) mReader.skip(count * sizeof(int32_t)); 170 } 171 172 float* getFloats(int& count) { 173 count = getInt(); 174 return (float*) mReader.skip(count * sizeof(float)); 175 } 176 177 void getText(TextContainer* text) { 178 size_t length = text->mByteLength = getInt(); 179 text->mText = (const char*) mReader.skip(length); 180 } 181 182 PathHeap* mPathHeap; 183 184 SkBitmap* mBitmaps; 185 int mBitmapCount; 186 187 SkMatrix* mMatrices; 188 int mMatrixCount; 189 190 SkPaint* mPaints; 191 int mPaintCount; 192 193 mutable SkFlattenableReadBuffer mReader; 194 195 SkRefCntPlayback mRCPlayback; 196 SkTypefacePlayback mTFPlayback; 197}; 198 199/////////////////////////////////////////////////////////////////////////////// 200// Renderer 201/////////////////////////////////////////////////////////////////////////////// 202 203/** 204 * Records drawing commands in a display list for latter playback. 205 */ 206class DisplayListRenderer: public OpenGLRenderer { 207public: 208 DisplayListRenderer(); 209 ~DisplayListRenderer(); 210 211 void setViewport(int width, int height); 212 void prepare(); 213 214 void acquireContext(); 215 void releaseContext(); 216 217 int save(int flags); 218 void restore(); 219 void restoreToCount(int saveCount); 220 221 int saveLayer(float left, float top, float right, float bottom, 222 const SkPaint* p, int flags); 223 224 void translate(float dx, float dy); 225 void rotate(float degrees); 226 void scale(float sx, float sy); 227 228 void setMatrix(SkMatrix* matrix); 229 void concatMatrix(SkMatrix* matrix); 230 231 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); 232 233 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint); 234 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint); 235 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, 236 float srcRight, float srcBottom, float dstLeft, float dstTop, 237 float dstRight, float dstBottom, const SkPaint* paint); 238 void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 239 uint32_t width, uint32_t height, float left, float top, float right, float bottom, 240 const SkPaint* paint); 241 void drawColor(int color, SkXfermode::Mode mode); 242 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint); 243 void drawPath(SkPath* path, SkPaint* paint); 244 void drawLines(float* points, int count, const SkPaint* paint); 245 void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint); 246 247 void resetShader(); 248 void setupShader(SkiaShader* shader); 249 250 void resetColorFilter(); 251 void setupColorFilter(SkiaColorFilter* filter); 252 253 void resetShadow(); 254 void setupShadow(float radius, float dx, float dy, int color); 255 256 void reset(); 257 258 DisplayList* getDisplayList() const { 259 return new DisplayList(*this); 260 } 261 262 const SkWriter32& writeStream() const { 263 return mWriter; 264 } 265 266 const SkTDArray<const SkFlatBitmap*>& getBitmaps() const { 267 return mBitmaps; 268 } 269 270 const SkTDArray<const SkFlatMatrix*>& getMatrices() const { 271 return mMatrices; 272 } 273 274 const SkTDArray<const SkFlatPaint*>& getPaints() const { 275 return mPaints; 276 } 277 278private: 279 inline void addOp(DisplayList::Op drawOp) { 280 mWriter.writeInt(drawOp); 281 } 282 283 inline void addInt(int value) { 284 mWriter.writeInt(value); 285 } 286 287 void addInts(const int32_t* values, uint32_t count) { 288 mWriter.writeInt(count); 289 for (uint32_t i = 0; i < count; i++) { 290 mWriter.writeInt(values[i]); 291 } 292 } 293 294 inline void addFloat(float value) { 295 mWriter.writeScalar(value); 296 } 297 298 void addFloats(const float* values, int count) { 299 mWriter.writeInt(count); 300 for (int i = 0; i < count; i++) { 301 mWriter.writeScalar(values[i]); 302 } 303 } 304 305 inline void addPoint(float x, float y) { 306 mWriter.writeScalar(x); 307 mWriter.writeScalar(y); 308 } 309 310 inline void addBounds(float left, float top, float right, float bottom) { 311 mWriter.writeScalar(left); 312 mWriter.writeScalar(top); 313 mWriter.writeScalar(right); 314 mWriter.writeScalar(bottom); 315 } 316 317 inline void addText(const void* text, size_t byteLength) { 318 mWriter.writeInt(byteLength); 319 mWriter.writePad(text, byteLength); 320 } 321 322 inline void addPath(const SkPath* path) { 323 if (mPathHeap == NULL) { 324 mPathHeap = new PathHeap(); 325 } 326 addInt(mPathHeap->append(*path)); 327 } 328 329 int find(SkTDArray<const SkFlatPaint*>& paints, const SkPaint* paint); 330 331 inline void addPaint(const SkPaint* paint) { 332 addInt(find(mPaints, paint)); 333 } 334 335 int find(SkTDArray<const SkFlatMatrix*>& matrices, const SkMatrix* matrix); 336 337 inline void addMatrix(const SkMatrix* matrix) { 338 addInt(find(mMatrices, matrix)); 339 } 340 341 int find(SkTDArray<const SkFlatBitmap*>& bitmaps, const SkBitmap& bitmap); 342 343 inline void addBitmap(const SkBitmap* bitmap) { 344 addInt(find(mBitmaps, *bitmap)); 345 } 346 347 SkChunkAlloc mHeap; 348 349 int mBitmapIndex; 350 SkTDArray<const SkFlatBitmap*> mBitmaps; 351 352 int mMatrixIndex; 353 SkTDArray<const SkFlatMatrix*> mMatrices; 354 355 int mPaintIndex; 356 SkTDArray<const SkFlatPaint*> mPaints; 357 358 PathHeap* mPathHeap; 359 SkWriter32 mWriter; 360 361 SkRefCntRecorder mRCRecorder; 362 SkRefCntRecorder mTFRecorder; 363 364 friend class DisplayList; 365 366}; // class DisplayListRenderer 367 368}; // namespace uirenderer 369}; // namespace android 370 371#endif // ANDROID_UI_DISPLAY_LIST_RENDERER_H 372