RecordingCanvas.h revision b565df13a9e5c7b1d7d93bdfa4a793752d66d3cc
1b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik/*
2b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Copyright (C) 2015 The Android Open Source Project
3b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
4b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Licensed under the Apache License, Version 2.0 (the "License");
5b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * you may not use this file except in compliance with the License.
6b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * You may obtain a copy of the License at
7b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
8b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *      http://www.apache.org/licenses/LICENSE-2.0
9b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
10b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Unless required by applicable law or agreed to in writing, software
11b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * distributed under the License is distributed on an "AS IS" BASIS,
12b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * See the License for the specific language governing permissions and
14b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * limitations under the License.
15b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik */
16b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
17b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#ifndef ANDROID_HWUI_RECORDING_CANVAS_H
18b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#define ANDROID_HWUI_RECORDING_CANVAS_H
19b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
20b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "Canvas.h"
21b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "CanvasState.h"
22b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "DisplayList.h"
23b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "utils/LinearAllocator.h"
24b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "utils/NinePatch.h"
25b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "ResourceCache.h"
26b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "SkiaCanvasProxy.h"
27b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "Snapshot.h"
28b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
29b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "SkDrawFilter.h"
30b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include <vector>
31b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
32b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace android {
33b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace uirenderer {
34b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
35b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikclass OpReceiver;
36b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikstruct RecordedOp;
37b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
38b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikclass RecordingCanvas: public Canvas, public CanvasStateClient {
39b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikpublic:
40b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    RecordingCanvas(size_t width, size_t height);
41b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual ~RecordingCanvas();
42b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
43b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    void reset(int width, int height);
44b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    DisplayListData* finishRecording();
45b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
46b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
47b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// MISC HWUI OPERATIONS - TODO: CATEGORIZE
48b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
49b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    void insertReorderBarrier(bool enableReorder) {}
50b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    void drawRenderNode(RenderNode* renderNode);
51b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
52b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
53b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// CanvasStateClient interface
54b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
55b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void onViewportInitialized() override {}
56b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override {}
57b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual GLuint getTargetFbo() const override { return -1; }
58b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
59b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
60b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// android/graphics/Canvas interface
61b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
62b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual SkCanvas* asSkCanvas() override;
63b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
64b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void setBitmap(const SkBitmap& bitmap) override {
65b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        LOG_ALWAYS_FATAL("RecordingCanvas is not backed by a bitmap.");
66b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
67b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
68b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool isOpaque() override { return false; }
69b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual int width() override { return mState.getWidth(); }
70b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual int height() override { return mState.getHeight(); }
71b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
72b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void setHighContrastText(bool highContrastText) override {
73b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mHighContrastText = highContrastText;
74b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
75b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool isHighContrastText() override { return mHighContrastText; }
76b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
77b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
78b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// android/graphics/Canvas state operations
79b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
80b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Save (layer)
81b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual int getSaveCount() const override { return mState.getSaveCount(); }
82b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual int save(SkCanvas::SaveFlags flags) override;
83b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void restore() override;
84b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void restoreToCount(int saveCount) override;
85b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
86b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
87b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkCanvas::SaveFlags flags) override;
88b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
89b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            int alpha, SkCanvas::SaveFlags flags) override {
90b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkPaint paint;
91b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        paint.setAlpha(alpha);
92b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return saveLayer(left, top, right, bottom, &paint, flags);
93b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
94b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
95b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Matrix
96b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void getMatrix(SkMatrix* outMatrix) const override { mState.getMatrix(outMatrix); }
97b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void setMatrix(const SkMatrix& matrix) override { mState.setMatrix(matrix); }
98b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
99b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void concat(const SkMatrix& matrix) override { mState.concatMatrix(matrix); }
100b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void rotate(float degrees) override;
101b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void scale(float sx, float sy) override;
102b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void skew(float sx, float sy) override;
103b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void translate(float dx, float dy) override;
104b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
105b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Clip
106b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool getClipBounds(SkRect* outRect) const override;
107b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
108b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool quickRejectPath(const SkPath& path) const override;
109b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
110b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) override;
111b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
112b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
113b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
114b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Misc
115b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual SkDrawFilter* getDrawFilter() override { return mDrawFilter.get(); }
116b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void setDrawFilter(SkDrawFilter* filter) override {
117b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mDrawFilter.reset(SkSafeRef(filter));
118b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
119b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
120b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
121b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// android/graphics/Canvas draw operations
122b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
123b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawColor(int color, SkXfermode::Mode mode) override;
124b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawPaint(const SkPaint& paint) override;
125b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
126b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Geometry
127b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawPoint(float x, float y, const SkPaint& paint) override {
128b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float points[2] = { x, y };
129b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawPoints(points, 2, paint);
130b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
131b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
132b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawLine(float startX, float startY, float stopX, float stopY,
133b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint& paint) override {
134b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float points[4] = { startX, startY, stopX, stopY };
135b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawLines(points, 4, paint);
136b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
137b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
138b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawRect(float left, float top, float right, float bottom, const SkPaint& paint) override;
139b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
140b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawRoundRect(float left, float top, float right, float bottom,
141b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float rx, float ry, const SkPaint& paint) override;
142b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
143b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawOval(float left, float top, float right, float bottom, const SkPaint& paint) override;
144b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawArc(float left, float top, float right, float bottom,
145b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
146b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
147b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
148b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const float* verts, const float* tex, const int* colors,
149b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const uint16_t* indices, int indexCount, const SkPaint& paint) override
150b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        { /* RecordingCanvas does not support drawVertices(); ignore */ }
151b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
152b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Bitmap-based
153b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) override;
154b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
155b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik                            const SkPaint* paint) override;
156b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
157b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float srcRight, float srcBottom, float dstLeft, float dstTop,
158b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float dstRight, float dstBottom, const SkPaint* paint) override;
159b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
160b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const float* vertices, const int* colors, const SkPaint* paint) override;
161b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
162b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float dstLeft, float dstTop, float dstRight, float dstBottom,
163b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint* paint) override;
164b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
165b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // Text
166b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
167b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop,
168b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float boundsRight, float boundsBottom, float totalAdvance) override;
169b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawPosText(const uint16_t* text, const float* positions, int count,
170b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            int posCount, const SkPaint& paint) override;
171b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
172b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float hOffset, float vOffset, const SkPaint& paint) override;
173b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    virtual bool drawTextAbsolutePos() const override { return false; }
174b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
175b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikprivate:
176b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    enum DeferredBarrierType {
177b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        kBarrier_None,
178b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        kBarrier_InOrder,
179b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        kBarrier_OutOfOrder,
180b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    };
181b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
182b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint);
183b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    void drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint);
184b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
185b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
186b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    size_t addOp(RecordedOp* op);
187b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
188b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// lazy object copy
189b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
190b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    LinearAllocator& alloc() { return mDisplayListData->allocator; }
191b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
192b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    void refBitmapsInShader(const SkShader* shader);
193b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
194b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    template<class T>
195b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline const T* refBuffer(const T* srcBuffer, int32_t count) {
196b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (!srcBuffer) return nullptr;
197b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
198b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        T* dstBuffer = (T*) mDisplayListData->allocator.alloc(count * sizeof(T));
199b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        memcpy(dstBuffer, srcBuffer, count * sizeof(T));
200b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return dstBuffer;
201b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
202b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
203b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline char* refText(const char* text, size_t byteLength) {
204b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return (char*) refBuffer<uint8_t>((uint8_t*)text, byteLength);
205b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
206b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
207b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline const SkPath* refPath(const SkPath* path) {
208b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (!path) return nullptr;
209b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
210b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // The points/verbs within the path are refcounted so this copy operation
211b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // is inexpensive and maintains the generationID of the original path.
212b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        const SkPath* cachedPath = new SkPath(*path);
213b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mDisplayListData->pathResources.push_back(cachedPath);
214b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return cachedPath;
215b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
216b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
217b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline const SkPaint* refPaint(const SkPaint* paint) {
218b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (!paint) return nullptr;
219b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
220b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // If there is a draw filter apply it here and store the modified paint
221b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // so that we don't need to modify the paint every time we access it.
222b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkTLazy<SkPaint> filteredPaint;
223b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (mDrawFilter.get()) {
224b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            filteredPaint.set(*paint);
225b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type);
226b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            paint = filteredPaint.get();
227b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
228b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
229b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // compute the hash key for the paint and check the cache.
230b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        const uint32_t key = paint->getHash();
231b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        const SkPaint* cachedPaint = mPaintMap.valueFor(key);
232b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // In the unlikely event that 2 unique paints have the same hash we do a
233b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // object equality check to ensure we don't erroneously dedup them.
234b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (cachedPaint == nullptr || *cachedPaint != *paint) {
235b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            cachedPaint = new SkPaint(*paint);
236b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            std::unique_ptr<const SkPaint> copy(cachedPaint);
237b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            mDisplayListData->paints.push_back(std::move(copy));
238b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
239b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            // replaceValueFor() performs an add if the entry doesn't exist
240b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            mPaintMap.replaceValueFor(key, cachedPaint);
241b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            refBitmapsInShader(cachedPaint->getShader());
242b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
243b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
244b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return cachedPaint;
245b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
246b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
247b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline const SkRegion* refRegion(const SkRegion* region) {
248b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (!region) {
249b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            return region;
250b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
251b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
252b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        const SkRegion* cachedRegion = mRegionMap.valueFor(region);
253b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // TODO: Add generation ID to SkRegion
254b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        if (cachedRegion == nullptr) {
255b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            std::unique_ptr<const SkRegion> copy(new SkRegion(*region));
256b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            cachedRegion = copy.get();
257b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            mDisplayListData->regions.push_back(std::move(copy));
258b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
259b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            // replaceValueFor() performs an add if the entry doesn't exist
260b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            mRegionMap.replaceValueFor(region, cachedRegion);
261b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
262b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
263b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return cachedRegion;
264b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
265b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
266b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline const SkBitmap* refBitmap(const SkBitmap& bitmap) {
267b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // Note that this assumes the bitmap is immutable. There are cases this won't handle
268b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // correctly, such as creating the bitmap from scratch, drawing with it, changing its
269b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // contents, and drawing again. The only fix would be to always copy it the first time,
270b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // which doesn't seem worth the extra cycles for this unlikely case.
271b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkBitmap* localBitmap = new (alloc()) SkBitmap(bitmap);
272b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        alloc().autoDestroy(localBitmap);
273b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mDisplayListData->bitmapResources.push_back(localBitmap);
274b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return localBitmap;
275b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
276b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
277b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    inline const Res_png_9patch* refPatch(const Res_png_9patch* patch) {
278b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mDisplayListData->patchResources.push_back(patch);
279b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mResourceCache.incrementRefcount(patch);
280b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        return patch;
281b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
282b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
283b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    DefaultKeyedVector<uint32_t, const SkPaint*> mPaintMap;
284b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap;
285b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap;
286b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
287b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    CanvasState mState;
288b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    std::unique_ptr<SkiaCanvasProxy> mSkiaCanvasProxy;
289b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    ResourceCache& mResourceCache;
290b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    DeferredBarrierType mDeferredBarrierType = kBarrier_None;
291b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    DisplayListData* mDisplayListData = nullptr;
292b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    bool mHighContrastText = false;
293b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    SkAutoTUnref<SkDrawFilter> mDrawFilter;
294b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    int mRestoreSaveCount = -1;
295b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}; // class RecordingCanvas
296b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
297b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}; // namespace uirenderer
298b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}; // namespace android
299b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
300b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif // ANDROID_HWUI_RECORDING_CANVAS_H
301