RecordedOp.h revision a1717271caac5e8ea3808c331d4141ac01a42134
1/*
2 * Copyright (C) 2015 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_RECORDED_OP_H
18#define ANDROID_HWUI_RECORDED_OP_H
19
20#include "font/FontUtil.h"
21#include "Matrix.h"
22#include "Rect.h"
23#include "RenderNode.h"
24#include "utils/LinearAllocator.h"
25#include "Vector.h"
26
27#include "SkXfermode.h"
28
29class SkBitmap;
30class SkPaint;
31
32namespace android {
33namespace uirenderer {
34
35class OffscreenBuffer;
36class RenderNode;
37struct Vertex;
38
39/**
40 * The provided macro is executed for each op type in order, with the results separated by commas.
41 *
42 * This serves as the authoritative list of ops, used for generating ID enum, and ID based LUTs.
43 */
44#define MAP_OPS(OP_FN) \
45        OP_FN(BitmapOp) \
46        OP_FN(LinesOp) \
47        OP_FN(RectOp) \
48        OP_FN(RenderNodeOp) \
49        OP_FN(ShadowOp) \
50        OP_FN(SimpleRectsOp) \
51        OP_FN(TextOp) \
52        OP_FN(BeginLayerOp) \
53        OP_FN(EndLayerOp) \
54        OP_FN(LayerOp)
55
56// Generate OpId enum
57#define IDENTITY_FN(Type) Type,
58namespace RecordedOpId {
59    enum {
60        MAP_OPS(IDENTITY_FN)
61        Count,
62    };
63}
64static_assert(RecordedOpId::BitmapOp == 0,
65        "First index must be zero for LUTs to work");
66
67#define BASE_PARAMS const Rect& unmappedBounds, const Matrix4& localMatrix, const Rect& localClipRect, const SkPaint* paint
68#define BASE_PARAMS_PAINTLESS const Rect& unmappedBounds, const Matrix4& localMatrix, const Rect& localClipRect
69#define SUPER(Type) RecordedOp(RecordedOpId::Type, unmappedBounds, localMatrix, localClipRect, paint)
70#define SUPER_PAINTLESS(Type) RecordedOp(RecordedOpId::Type, unmappedBounds, localMatrix, localClipRect, nullptr)
71
72struct RecordedOp {
73    /* ID from RecordedOpId - generally used for jumping into function tables */
74    const int opId;
75
76    /* bounds in *local* space, without accounting for DisplayList transformation */
77    const Rect unmappedBounds;
78
79    /* transform in recording space (vs DisplayList origin) */
80    const Matrix4 localMatrix;
81
82    /* clip in recording space */
83    const Rect localClipRect;
84
85    /* optional paint, stored in base object to simplify merging logic */
86    const SkPaint* paint;
87protected:
88    RecordedOp(unsigned int opId, BASE_PARAMS)
89            : opId(opId)
90            , unmappedBounds(unmappedBounds)
91            , localMatrix(localMatrix)
92            , localClipRect(localClipRect)
93            , paint(paint) {}
94};
95
96struct RenderNodeOp : RecordedOp {
97    RenderNodeOp(BASE_PARAMS_PAINTLESS, RenderNode* renderNode)
98            : SUPER_PAINTLESS(RenderNodeOp)
99            , renderNode(renderNode) {}
100    RenderNode * renderNode; // not const, since drawing modifies it (somehow...)
101    bool skipInOrderDraw = false;
102};
103
104////////////////////////////////////////////////////////////////////////////////////////////////////
105// Standard Ops
106////////////////////////////////////////////////////////////////////////////////////////////////////
107
108struct BitmapOp : RecordedOp {
109    BitmapOp(BASE_PARAMS, const SkBitmap* bitmap)
110            : SUPER(BitmapOp)
111            , bitmap(bitmap) {}
112    const SkBitmap* bitmap;
113    // TODO: asset atlas/texture id lookup?
114};
115
116struct LinesOp : RecordedOp {
117    LinesOp(BASE_PARAMS, const float* points, const int floatCount)
118            : SUPER(LinesOp)
119            , points(points)
120            , floatCount(floatCount) {}
121    const float* points;
122    const int floatCount;
123};
124
125struct RectOp : RecordedOp {
126    RectOp(BASE_PARAMS)
127            : SUPER(RectOp) {}
128};
129
130/**
131 * Real-time, dynamic-lit shadow.
132 *
133 * Uses invalid/empty bounds and matrix since ShadowOp bounds aren't known at defer time,
134 * and are resolved dynamically, and transform isn't needed.
135 *
136 * State construction handles these properties specially, ignoring matrix/bounds.
137 */
138struct ShadowOp : RecordedOp {
139    ShadowOp(const RenderNodeOp& casterOp, float casterAlpha, const SkPath* casterPath,
140            const Rect& clipRect, const Vector3& lightCenter)
141            : RecordedOp(RecordedOpId::ShadowOp, Rect(), Matrix4::identity(), clipRect, nullptr)
142            , shadowMatrixXY(casterOp.localMatrix)
143            , shadowMatrixZ(casterOp.localMatrix)
144            , casterAlpha(casterAlpha)
145            , casterPath(casterPath)
146            , lightCenter(lightCenter) {
147        const RenderNode& node = *casterOp.renderNode;
148        node.applyViewPropertyTransforms(shadowMatrixXY, false);
149        node.applyViewPropertyTransforms(shadowMatrixZ, true);
150    };
151    Matrix4 shadowMatrixXY;
152    Matrix4 shadowMatrixZ;
153    const float casterAlpha;
154    const SkPath* casterPath;
155    const Vector3 lightCenter;
156};
157
158struct SimpleRectsOp : RecordedOp { // Filled, no AA (TODO: better name?)
159    SimpleRectsOp(BASE_PARAMS, Vertex* vertices, size_t vertexCount)
160            : SUPER(SimpleRectsOp)
161            , vertices(vertices)
162            , vertexCount(vertexCount) {}
163    Vertex* vertices;
164    const size_t vertexCount;
165};
166
167struct TextOp : RecordedOp {
168    TextOp(BASE_PARAMS, const glyph_t* glyphs, const float* positions, int glyphCount,
169            float x, float y)
170            : SUPER(TextOp)
171            , glyphs(glyphs)
172            , positions(positions)
173            , glyphCount(glyphCount)
174            , x(x)
175            , y(y) {}
176    const glyph_t* glyphs;
177    const float* positions;
178    const int glyphCount;
179    const float x;
180    const float y;
181};
182
183////////////////////////////////////////////////////////////////////////////////////////////////////
184// Layers
185////////////////////////////////////////////////////////////////////////////////////////////////////
186
187
188/**
189 * Stateful operation! denotes the creation of an off-screen layer,
190 * and that commands following will render into it.
191 */
192struct BeginLayerOp : RecordedOp {
193    BeginLayerOp(BASE_PARAMS)
194            : SUPER(BeginLayerOp) {}
195};
196
197/**
198 * Stateful operation! Denotes end of off-screen layer, and that
199 * commands since last BeginLayerOp should be drawn into parent FBO.
200 *
201 * State in this op is empty, it just serves to signal that a layer has been finished.
202 */
203struct EndLayerOp : RecordedOp {
204    EndLayerOp()
205            : RecordedOp(RecordedOpId::EndLayerOp, Rect(0, 0), Matrix4::identity(), Rect(0, 0), nullptr) {}
206};
207
208/**
209 * Draws an OffscreenBuffer.
210 *
211 * Alpha, mode, and colorfilter are embedded, since LayerOps are always dynamically generated,
212 * when creating/tracking a SkPaint* during defer isn't worth the bother.
213 */
214struct LayerOp : RecordedOp {
215    // Records a one-use (saveLayer) layer for drawing. Once drawn, the layer will be destroyed.
216    LayerOp(BASE_PARAMS, OffscreenBuffer** layerHandle)
217            : SUPER_PAINTLESS(LayerOp)
218            , layerHandle(layerHandle)
219            , alpha(paint->getAlpha() / 255.0f)
220            , mode(PaintUtils::getXfermodeDirect(paint))
221            , colorFilter(paint->getColorFilter())
222            , destroy(true) {}
223
224    LayerOp(RenderNode& node)
225        : RecordedOp(RecordedOpId::LayerOp, Rect(node.getWidth(), node.getHeight()), Matrix4::identity(), Rect(node.getWidth(), node.getHeight()), nullptr)
226        , layerHandle(node.getLayerHandle())
227        , alpha(node.properties().layerProperties().alpha() / 255.0f)
228        , mode(node.properties().layerProperties().xferMode())
229        , colorFilter(node.properties().layerProperties().colorFilter())
230        , destroy(false) {}
231
232    // Records a handle to the Layer object, since the Layer itself won't be
233    // constructed until after this operation is constructed.
234    OffscreenBuffer** layerHandle;
235    const float alpha;
236    const SkXfermode::Mode mode;
237
238    // pointer to object owned by either LayerProperties, or a recorded Paint object in a
239    // BeginLayerOp. Lives longer than LayerOp in either case, so no skia ref counting is used.
240    SkColorFilter* colorFilter;
241
242    // whether to destroy the layer, once rendered
243    const bool destroy;
244};
245
246}; // namespace uirenderer
247}; // namespace android
248
249#endif // ANDROID_HWUI_RECORDED_OP_H
250