DisplayListCanvas.h revision 8d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7
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_HWUI_DISPLAY_LIST_RENDERER_H 18#define ANDROID_HWUI_DISPLAY_LIST_RENDERER_H 19 20#include "Canvas.h" 21#include "CanvasState.h" 22#include "DisplayList.h" 23#include "RenderNode.h" 24#include "ResourceCache.h" 25#include "SkiaCanvasProxy.h" 26#include "utils/Macros.h" 27 28#include <SkDrawFilter.h> 29#include <SkMatrix.h> 30#include <SkPaint.h> 31#include <SkPath.h> 32#include <SkRegion.h> 33#include <SkTLazy.h> 34#include <cutils/compiler.h> 35 36namespace android { 37namespace uirenderer { 38 39/////////////////////////////////////////////////////////////////////////////// 40// Defines 41/////////////////////////////////////////////////////////////////////////////// 42 43// Debug 44#if DEBUG_DISPLAY_LIST 45 #define DISPLAY_LIST_LOGD(...) ALOGD(__VA_ARGS__) 46#else 47 #define DISPLAY_LIST_LOGD(...) 48#endif 49 50/////////////////////////////////////////////////////////////////////////////// 51// Display list 52/////////////////////////////////////////////////////////////////////////////// 53 54class DeferredDisplayList; 55class DeferredLayerUpdater; 56class DisplayListOp; 57class DrawOp; 58class DrawRenderNodeOp; 59class RenderNode; 60class StateOp; 61 62/** 63 * Records drawing commands in a display list for later playback into an OpenGLRenderer. 64 */ 65class ANDROID_API DisplayListCanvas: public Canvas, public CanvasStateClient { 66public: 67 DisplayListCanvas(int width, int height); 68 virtual ~DisplayListCanvas(); 69 70 void reset(int width, int height); 71 WARN_UNUSED_RESULT DisplayList* finishRecording(); 72 73// ---------------------------------------------------------------------------- 74// HWUI Canvas state operations 75// ---------------------------------------------------------------------------- 76 77 void insertReorderBarrier(bool enableReorder); 78 79// ---------------------------------------------------------------------------- 80// HWUI Canvas draw operations 81// ---------------------------------------------------------------------------- 82 83 // Shapes 84 void drawRoundRect(CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top, 85 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom, 86 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry, 87 CanvasPropertyPaint* paint); 88 void drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, 89 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint); 90 91 92// ---------------------------------------------------------------------------- 93// HWUI Canvas draw operations - special 94// ---------------------------------------------------------------------------- 95 void drawLayer(DeferredLayerUpdater* layerHandle); 96 void drawRenderNode(RenderNode* renderNode); 97 98 // TODO: rename for consistency 99 void callDrawGLFunction(Functor* functor); 100 101// ---------------------------------------------------------------------------- 102// CanvasStateClient interface 103// ---------------------------------------------------------------------------- 104 virtual void onViewportInitialized() override { } 105 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override { } 106 virtual GLuint getTargetFbo() const override { return -1; } 107 108// ---------------------------------------------------------------------------- 109// android/graphics/Canvas interface 110// ---------------------------------------------------------------------------- 111 virtual SkCanvas* asSkCanvas() override; 112 113 virtual void setBitmap(const SkBitmap& bitmap) override { 114 LOG_ALWAYS_FATAL("DisplayListCanvas is not backed by a bitmap."); 115 } 116 117 virtual bool isOpaque() override { return false; } 118 virtual int width() override { return mState.getWidth(); } 119 virtual int height() override { return mState.getHeight(); } 120 121 virtual void setHighContrastText(bool highContrastText) override { 122 mHighContrastText = highContrastText; 123 } 124 virtual bool isHighContrastText() override { return mHighContrastText; } 125 126// ---------------------------------------------------------------------------- 127// android/graphics/Canvas state operations 128// ---------------------------------------------------------------------------- 129 // Save (layer) 130 virtual int getSaveCount() const override { return mState.getSaveCount(); } 131 virtual int save(SkCanvas::SaveFlags flags) override; 132 virtual void restore() override; 133 virtual void restoreToCount(int saveCount) override; 134 135 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint, 136 SkCanvas::SaveFlags flags) override; 137 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 138 int alpha, SkCanvas::SaveFlags flags) override { 139 SkPaint paint; 140 paint.setAlpha(alpha); 141 return saveLayer(left, top, right, bottom, &paint, flags); 142 } 143 144 // Matrix 145 virtual void getMatrix(SkMatrix* outMatrix) const override { mState.getMatrix(outMatrix); } 146 virtual void setMatrix(const SkMatrix& matrix) override; 147 148 virtual void concat(const SkMatrix& matrix) override; 149 virtual void rotate(float degrees) override; 150 virtual void scale(float sx, float sy) override; 151 virtual void skew(float sx, float sy) override; 152 virtual void translate(float dx, float dy) override; 153 154 // Clip 155 virtual bool getClipBounds(SkRect* outRect) const override; 156 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; 157 virtual bool quickRejectPath(const SkPath& path) const override; 158 159 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) override; 160 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override; 161 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override; 162 163 // Misc 164 virtual SkDrawFilter* getDrawFilter() override { return mDrawFilter.get(); } 165 virtual void setDrawFilter(SkDrawFilter* filter) override; 166 167// ---------------------------------------------------------------------------- 168// android/graphics/Canvas draw operations 169// ---------------------------------------------------------------------------- 170 virtual void drawColor(int color, SkXfermode::Mode mode) override; 171 virtual void drawPaint(const SkPaint& paint) override; 172 173 // Geometry 174 virtual void drawPoint(float x, float y, const SkPaint& paint) override { 175 float points[2] = { x, y }; 176 drawPoints(points, 2, paint); 177 } 178 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override; 179 virtual void drawLine(float startX, float startY, float stopX, float stopY, 180 const SkPaint& paint) override { 181 float points[4] = { startX, startY, stopX, stopY }; 182 drawLines(points, 4, paint); 183 } 184 virtual void drawLines(const float* points, int count, const SkPaint& paint) override; 185 virtual void drawRect(float left, float top, float right, float bottom, const SkPaint& paint) override; 186 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override; 187 virtual void drawRoundRect(float left, float top, float right, float bottom, 188 float rx, float ry, const SkPaint& paint) override; 189 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override; 190 virtual void drawOval(float left, float top, float right, float bottom, const SkPaint& paint) override; 191 virtual void drawArc(float left, float top, float right, float bottom, 192 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override; 193 virtual void drawPath(const SkPath& path, const SkPaint& paint) override; 194 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, 195 const float* verts, const float* tex, const int* colors, 196 const uint16_t* indices, int indexCount, const SkPaint& paint) override 197 { /* DisplayListCanvas does not support drawVertices(); ignore */ } 198 199 // Bitmap-based 200 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) override; 201 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, 202 const SkPaint* paint) override; 203 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, 204 float srcRight, float srcBottom, float dstLeft, float dstTop, 205 float dstRight, float dstBottom, const SkPaint* paint) override; 206 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, 207 const float* vertices, const int* colors, const SkPaint* paint) override; 208 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk, 209 float dstLeft, float dstTop, float dstRight, float dstBottom, 210 const SkPaint* paint) override; 211 212 // Text 213 virtual void drawText(const uint16_t* glyphs, const float* positions, int count, 214 const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop, 215 float boundsRight, float boundsBottom, float totalAdvance) override; 216 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path, 217 float hOffset, float vOffset, const SkPaint& paint) override; 218 virtual bool drawTextAbsolutePos() const override { return false; } 219 220 221private: 222 223 CanvasState mState; 224 std::unique_ptr<SkiaCanvasProxy> mSkiaCanvasProxy; 225 226 enum DeferredBarrierType { 227 kBarrier_None, 228 kBarrier_InOrder, 229 kBarrier_OutOfOrder, 230 }; 231 232 void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint); 233 void drawRects(const float* rects, int count, const SkPaint* paint); 234 235 void flushRestoreToCount(); 236 void flushTranslate(); 237 void flushReorderBarrier(); 238 239 LinearAllocator& alloc() { return mDisplayList->allocator; } 240 241 // Each method returns final index of op 242 size_t addOpAndUpdateChunk(DisplayListOp* op); 243 // flushes any deferred operations, and appends the op 244 size_t flushAndAddOp(DisplayListOp* op); 245 246 size_t addStateOp(StateOp* op); 247 size_t addDrawOp(DrawOp* op); 248 size_t addRenderNodeOp(DrawRenderNodeOp* op); 249 250 void refBitmapsInShader(const SkShader* shader); 251 252 template<class T> 253 inline const T* refBuffer(const T* srcBuffer, int32_t count) { 254 if (!srcBuffer) return nullptr; 255 256 T* dstBuffer = (T*) mDisplayList->allocator.alloc(count * sizeof(T)); 257 memcpy(dstBuffer, srcBuffer, count * sizeof(T)); 258 return dstBuffer; 259 } 260 261 inline char* refText(const char* text, size_t byteLength) { 262 return (char*) refBuffer<uint8_t>((uint8_t*)text, byteLength); 263 } 264 265 inline const SkPath* refPath(const SkPath* path) { 266 if (!path) return nullptr; 267 268 // The points/verbs within the path are refcounted so this copy operation 269 // is inexpensive and maintains the generationID of the original path. 270 const SkPath* cachedPath = new SkPath(*path); 271 mDisplayList->pathResources.push_back(cachedPath); 272 return cachedPath; 273 } 274 275 inline const SkPaint* refPaint(const SkPaint* paint) { 276 if (!paint) return nullptr; 277 278 // If there is a draw filter apply it here and store the modified paint 279 // so that we don't need to modify the paint every time we access it. 280 SkTLazy<SkPaint> filteredPaint; 281 if (mDrawFilter.get()) { 282 filteredPaint.set(*paint); 283 mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type); 284 paint = filteredPaint.get(); 285 } 286 287 // compute the hash key for the paint and check the cache. 288 const uint32_t key = paint->getHash(); 289 const SkPaint* cachedPaint = mPaintMap.valueFor(key); 290 // In the unlikely event that 2 unique paints have the same hash we do a 291 // object equality check to ensure we don't erroneously dedup them. 292 if (cachedPaint == nullptr || *cachedPaint != *paint) { 293 cachedPaint = new SkPaint(*paint); 294 std::unique_ptr<const SkPaint> copy(cachedPaint); 295 mDisplayList->paints.push_back(std::move(copy)); 296 297 // replaceValueFor() performs an add if the entry doesn't exist 298 mPaintMap.replaceValueFor(key, cachedPaint); 299 refBitmapsInShader(cachedPaint->getShader()); 300 } 301 302 return cachedPaint; 303 } 304 305 inline const SkRegion* refRegion(const SkRegion* region) { 306 if (!region) { 307 return region; 308 } 309 310 const SkRegion* cachedRegion = mRegionMap.valueFor(region); 311 // TODO: Add generation ID to SkRegion 312 if (cachedRegion == nullptr) { 313 std::unique_ptr<const SkRegion> copy(new SkRegion(*region)); 314 cachedRegion = copy.get(); 315 mDisplayList->regions.push_back(std::move(copy)); 316 317 // replaceValueFor() performs an add if the entry doesn't exist 318 mRegionMap.replaceValueFor(region, cachedRegion); 319 } 320 321 return cachedRegion; 322 } 323 324 inline const SkBitmap* refBitmap(const SkBitmap& bitmap) { 325 // Note that this assumes the bitmap is immutable. There are cases this won't handle 326 // correctly, such as creating the bitmap from scratch, drawing with it, changing its 327 // contents, and drawing again. The only fix would be to always copy it the first time, 328 // which doesn't seem worth the extra cycles for this unlikely case. 329 SkBitmap* localBitmap = new (alloc()) SkBitmap(bitmap); 330 alloc().autoDestroy(localBitmap); 331 mDisplayList->bitmapResources.push_back(localBitmap); 332 return localBitmap; 333 } 334 335 inline const Res_png_9patch* refPatch(const Res_png_9patch* patch) { 336 mDisplayList->patchResources.push_back(patch); 337 mResourceCache.incrementRefcount(patch); 338 return patch; 339 } 340 341 DefaultKeyedVector<uint32_t, const SkPaint*> mPaintMap; 342 DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap; 343 DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap; 344 345 ResourceCache& mResourceCache; 346 DisplayList* mDisplayList; 347 348 float mTranslateX; 349 float mTranslateY; 350 bool mHasDeferredTranslate; 351 DeferredBarrierType mDeferredBarrierType; 352 bool mHighContrastText; 353 354 int mRestoreSaveCount; 355 356 SkAutoTUnref<SkDrawFilter> mDrawFilter; 357 358 friend class RenderNode; 359 360}; // class DisplayListCanvas 361 362}; // namespace uirenderer 363}; // namespace android 364 365#endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H 366