RecordedOp.h revision 91eff22b5d7f8fe551bae01331948858ce932a96
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 "RecordedOp.h"
21#include "font/FontUtil.h"
22#include "Matrix.h"
23#include "Rect.h"
24#include "RenderNode.h"
25#include "TessellationCache.h"
26#include "utils/LinearAllocator.h"
27#include "Vector.h"
28
29#include <androidfw/ResourceTypes.h>
30#include <SkXfermode.h>
31
32class SkBitmap;
33class SkPaint;
34
35namespace android {
36namespace uirenderer {
37
38struct ClipBase;
39class OffscreenBuffer;
40class RenderNode;
41struct Vertex;
42
43namespace VectorDrawable {
44class Tree;
45}
46
47/**
48 * Authoritative op list, used for generating the op ID enum, ID based LUTS, and
49 * the functions to which they dispatch. Parameter macros are executed for each op,
50 * in order, based on the op's type.
51 *
52 * There are 4 types of op, which defines dispatch/LUT capability:
53 *
54 *              | DisplayList |   Render    |    Merge    |
55 * -------------|-------------|-------------|-------------|
56 * PRE RENDER   |     Yes     |             |             |
57 * RENDER ONLY  |             |     Yes     |             |
58 * UNMERGEABLE  |     Yes     |     Yes     |             |
59 * MERGEABLE    |     Yes     |     Yes     |     Yes     |
60 *
61 * PRE RENDER - These ops are recorded into DisplayLists, but can't be directly rendered. This
62 *      may be because they need to be transformed into other op types (e.g. CirclePropsOp),
63 *      be traversed to access multiple renderable ops within (e.g. RenderNodeOp), or because they
64 *      modify renderbuffer lifecycle, instead of directly rendering content (the various LayerOps).
65 *
66 * RENDER ONLY - These ops cannot be recorded into DisplayLists, and are instead implicitly
67 *      constructed from other commands/RenderNode properties. They cannot be merged.
68 *
69 * UNMERGEABLE - These ops can be recorded into DisplayLists and rendered directly, but do not
70 *      support merged rendering.
71 *
72 * MERGEABLE - These ops can be recorded into DisplayLists and rendered individually, or merged
73 *      under certain circumstances.
74 */
75#define MAP_OPS_BASED_ON_TYPE(PRE_RENDER_OP_FN, RENDER_ONLY_OP_FN, UNMERGEABLE_OP_FN, MERGEABLE_OP_FN) \
76        PRE_RENDER_OP_FN(RenderNodeOp) \
77        PRE_RENDER_OP_FN(CirclePropsOp) \
78        PRE_RENDER_OP_FN(RoundRectPropsOp) \
79        PRE_RENDER_OP_FN(BeginLayerOp) \
80        PRE_RENDER_OP_FN(EndLayerOp) \
81        PRE_RENDER_OP_FN(BeginUnclippedLayerOp) \
82        PRE_RENDER_OP_FN(EndUnclippedLayerOp) \
83        PRE_RENDER_OP_FN(VectorDrawableOp) \
84        \
85        RENDER_ONLY_OP_FN(ShadowOp) \
86        RENDER_ONLY_OP_FN(LayerOp) \
87        RENDER_ONLY_OP_FN(CopyToLayerOp) \
88        RENDER_ONLY_OP_FN(CopyFromLayerOp) \
89        \
90        UNMERGEABLE_OP_FN(ArcOp) \
91        UNMERGEABLE_OP_FN(BitmapMeshOp) \
92        UNMERGEABLE_OP_FN(BitmapRectOp) \
93        UNMERGEABLE_OP_FN(FunctorOp) \
94        UNMERGEABLE_OP_FN(LinesOp) \
95        UNMERGEABLE_OP_FN(OvalOp) \
96        UNMERGEABLE_OP_FN(PathOp) \
97        UNMERGEABLE_OP_FN(PointsOp) \
98        UNMERGEABLE_OP_FN(RectOp) \
99        UNMERGEABLE_OP_FN(RoundRectOp) \
100        UNMERGEABLE_OP_FN(SimpleRectsOp) \
101        UNMERGEABLE_OP_FN(TextOnPathOp) \
102        UNMERGEABLE_OP_FN(TextureLayerOp) \
103        \
104        MERGEABLE_OP_FN(BitmapOp) \
105        MERGEABLE_OP_FN(PatchOp) \
106        MERGEABLE_OP_FN(TextOp)
107
108/**
109 * LUT generators, which will insert nullptr for unsupported ops
110 */
111#define NULLPTR_OP_FN(Type) nullptr,
112
113#define BUILD_DEFERRABLE_OP_LUT(OP_FN) \
114        { MAP_OPS_BASED_ON_TYPE(OP_FN, NULLPTR_OP_FN, OP_FN, OP_FN) }
115
116#define BUILD_MERGEABLE_OP_LUT(OP_FN) \
117        { MAP_OPS_BASED_ON_TYPE(NULLPTR_OP_FN, NULLPTR_OP_FN, NULLPTR_OP_FN, OP_FN) }
118
119#define BUILD_RENDERABLE_OP_LUT(OP_FN) \
120        { MAP_OPS_BASED_ON_TYPE(NULLPTR_OP_FN, OP_FN, OP_FN, OP_FN) }
121
122#define BUILD_FULL_OP_LUT(OP_FN) \
123        { MAP_OPS_BASED_ON_TYPE(OP_FN, OP_FN, OP_FN, OP_FN) }
124
125/**
126 * Op mapping functions, which skip unsupported ops.
127 *
128 * Note: Do not use for LUTS, since these do not preserve ID order.
129 */
130#define NULL_OP_FN(Type)
131
132#define MAP_DEFERRABLE_OPS(OP_FN) \
133        MAP_OPS_BASED_ON_TYPE(OP_FN, NULL_OP_FN, OP_FN, OP_FN)
134
135#define MAP_MERGEABLE_OPS(OP_FN) \
136        MAP_OPS_BASED_ON_TYPE(NULL_OP_FN, NULL_OP_FN, NULL_OP_FN, OP_FN)
137
138#define MAP_RENDERABLE_OPS(OP_FN) \
139        MAP_OPS_BASED_ON_TYPE(NULL_OP_FN, OP_FN, OP_FN, OP_FN)
140
141// Generate OpId enum
142#define IDENTITY_FN(Type) Type,
143namespace RecordedOpId {
144    enum {
145        MAP_OPS_BASED_ON_TYPE(IDENTITY_FN, IDENTITY_FN, IDENTITY_FN, IDENTITY_FN)
146        Count,
147    };
148}
149static_assert(RecordedOpId::RenderNodeOp == 0,
150        "First index must be zero for LUTs to work");
151
152#define BASE_PARAMS const Rect& unmappedBounds, const Matrix4& localMatrix, const ClipBase* localClip, const SkPaint* paint
153#define BASE_PARAMS_PAINTLESS const Rect& unmappedBounds, const Matrix4& localMatrix, const ClipBase* localClip
154#define SUPER(Type) RecordedOp(RecordedOpId::Type, unmappedBounds, localMatrix, localClip, paint)
155#define SUPER_PAINTLESS(Type) RecordedOp(RecordedOpId::Type, unmappedBounds, localMatrix, localClip, nullptr)
156
157struct RecordedOp {
158    /* ID from RecordedOpId - generally used for jumping into function tables */
159    const int opId;
160
161    /* bounds in *local* space, without accounting for DisplayList transformation, or stroke */
162    const Rect unmappedBounds;
163
164    /* transform in recording space (vs DisplayList origin) */
165    const Matrix4 localMatrix;
166
167    /* clip in recording space - nullptr if not clipped */
168    const ClipBase* localClip;
169
170    /* optional paint, stored in base object to simplify merging logic */
171    const SkPaint* paint;
172protected:
173    RecordedOp(unsigned int opId, BASE_PARAMS)
174            : opId(opId)
175            , unmappedBounds(unmappedBounds)
176            , localMatrix(localMatrix)
177            , localClip(localClip)
178            , paint(paint) {}
179};
180
181struct RenderNodeOp : RecordedOp {
182    RenderNodeOp(BASE_PARAMS_PAINTLESS, RenderNode* renderNode)
183            : SUPER_PAINTLESS(RenderNodeOp)
184            , renderNode(renderNode) {}
185    RenderNode * renderNode; // not const, since drawing modifies it
186
187    /**
188     * Holds the transformation between the projection surface ViewGroup and this RenderNode
189     * drawing instance. Represents any translations / transformations done within the drawing of
190     * the compositing ancestor ViewGroup's draw, before the draw of the View represented by this
191     * DisplayList draw instance.
192     *
193     * Note: doesn't include transformation within the RenderNode, or its properties.
194     */
195    Matrix4 transformFromCompositingAncestor;
196    bool skipInOrderDraw = false;
197};
198
199////////////////////////////////////////////////////////////////////////////////////////////////////
200// Standard Ops
201////////////////////////////////////////////////////////////////////////////////////////////////////
202
203struct ArcOp : RecordedOp {
204    ArcOp(BASE_PARAMS, float startAngle, float sweepAngle, bool useCenter)
205            : SUPER(ArcOp)
206            , startAngle(startAngle)
207            , sweepAngle(sweepAngle)
208            , useCenter(useCenter) {}
209    const float startAngle;
210    const float sweepAngle;
211    const bool useCenter;
212};
213
214struct BitmapOp : RecordedOp {
215    BitmapOp(BASE_PARAMS, const SkBitmap* bitmap)
216            : SUPER(BitmapOp)
217            , bitmap(bitmap) {}
218    const SkBitmap* bitmap;
219    // TODO: asset atlas/texture id lookup?
220};
221
222struct BitmapMeshOp : RecordedOp {
223    BitmapMeshOp(BASE_PARAMS, const SkBitmap* bitmap, int meshWidth, int meshHeight,
224            const float* vertices, const int* colors)
225            : SUPER(BitmapMeshOp)
226            , bitmap(bitmap)
227            , meshWidth(meshWidth)
228            , meshHeight(meshHeight)
229            , vertices(vertices)
230            , colors(colors) {}
231    const SkBitmap* bitmap;
232    const int meshWidth;
233    const int meshHeight;
234    const float* vertices;
235    const int* colors;
236};
237
238struct BitmapRectOp : RecordedOp {
239    BitmapRectOp(BASE_PARAMS, const SkBitmap* bitmap, const Rect& src)
240            : SUPER(BitmapRectOp)
241            , bitmap(bitmap)
242            , src(src) {}
243    const SkBitmap* bitmap;
244    const Rect src;
245};
246
247struct CirclePropsOp : RecordedOp {
248    CirclePropsOp(const Matrix4& localMatrix, const ClipBase* localClip, const SkPaint* paint,
249            float* x, float* y, float* radius)
250            : RecordedOp(RecordedOpId::CirclePropsOp, Rect(), localMatrix, localClip, paint)
251            , x(x)
252            , y(y)
253            , radius(radius) {}
254    const float* x;
255    const float* y;
256    const float* radius;
257};
258
259struct FunctorOp : RecordedOp {
260    FunctorOp(BASE_PARAMS_PAINTLESS, Functor* functor)
261            : SUPER_PAINTLESS(FunctorOp)
262            , functor(functor) {}
263    Functor* functor;
264};
265
266struct LinesOp : RecordedOp {
267    LinesOp(BASE_PARAMS, const float* points, const int floatCount)
268            : SUPER(LinesOp)
269            , points(points)
270            , floatCount(floatCount) {}
271    const float* points;
272    const int floatCount;
273};
274
275struct OvalOp : RecordedOp {
276    OvalOp(BASE_PARAMS)
277            : SUPER(OvalOp) {}
278};
279
280struct PatchOp : RecordedOp {
281    PatchOp(BASE_PARAMS, const SkBitmap* bitmap, const Res_png_9patch* patch)
282            : SUPER(PatchOp)
283            , bitmap(bitmap)
284            , patch(patch) {}
285    const SkBitmap* bitmap;
286    const Res_png_9patch* patch;
287};
288
289struct PathOp : RecordedOp {
290    PathOp(BASE_PARAMS, const SkPath* path)
291            : SUPER(PathOp)
292            , path(path) {}
293    const SkPath* path;
294};
295
296struct PointsOp : RecordedOp {
297    PointsOp(BASE_PARAMS, const float* points, const int floatCount)
298            : SUPER(PointsOp)
299            , points(points)
300            , floatCount(floatCount) {}
301    const float* points;
302    const int floatCount;
303};
304
305struct RectOp : RecordedOp {
306    RectOp(BASE_PARAMS)
307            : SUPER(RectOp) {}
308};
309
310struct RoundRectOp : RecordedOp {
311    RoundRectOp(BASE_PARAMS, float rx, float ry)
312            : SUPER(RoundRectOp)
313            , rx(rx)
314            , ry(ry) {}
315    const float rx;
316    const float ry;
317};
318
319struct RoundRectPropsOp : RecordedOp {
320    RoundRectPropsOp(const Matrix4& localMatrix, const ClipBase* localClip, const SkPaint* paint,
321            float* left, float* top, float* right, float* bottom, float *rx, float *ry)
322            : RecordedOp(RecordedOpId::RoundRectPropsOp, Rect(), localMatrix, localClip, paint)
323            , left(left)
324            , top(top)
325            , right(right)
326            , bottom(bottom)
327            , rx(rx)
328            , ry(ry) {}
329    const float* left;
330    const float* top;
331    const float* right;
332    const float* bottom;
333    const float* rx;
334    const float* ry;
335};
336
337struct VectorDrawableOp : RecordedOp {
338    VectorDrawableOp(VectorDrawable::Tree* tree, BASE_PARAMS_PAINTLESS)
339            : SUPER_PAINTLESS(VectorDrawableOp)
340            , vectorDrawable(tree) {}
341    VectorDrawable::Tree* vectorDrawable;
342};
343
344/**
345 * Real-time, dynamic-lit shadow.
346 *
347 * Uses invalid/empty bounds and matrix since ShadowOp bounds aren't known at defer time,
348 * and are resolved dynamically, and transform isn't needed.
349 *
350 * State construction handles these properties specially, ignoring matrix/bounds.
351 */
352struct ShadowOp : RecordedOp {
353    ShadowOp(sp<TessellationCache::ShadowTask>& shadowTask, float casterAlpha)
354            : RecordedOp(RecordedOpId::ShadowOp, Rect(), Matrix4::identity(), nullptr, nullptr)
355            , shadowTask(shadowTask)
356            , casterAlpha(casterAlpha) {
357    };
358    sp<TessellationCache::ShadowTask> shadowTask;
359    const float casterAlpha;
360};
361
362struct SimpleRectsOp : RecordedOp { // Filled, no AA (TODO: better name?)
363    SimpleRectsOp(BASE_PARAMS, Vertex* vertices, size_t vertexCount)
364            : SUPER(SimpleRectsOp)
365            , vertices(vertices)
366            , vertexCount(vertexCount) {}
367    Vertex* vertices;
368    const size_t vertexCount;
369};
370
371struct TextOp : RecordedOp {
372    TextOp(BASE_PARAMS, const glyph_t* glyphs, const float* positions, int glyphCount,
373            float x, float y)
374            : SUPER(TextOp)
375            , glyphs(glyphs)
376            , positions(positions)
377            , glyphCount(glyphCount)
378            , x(x)
379            , y(y) {}
380    const glyph_t* glyphs;
381    const float* positions;
382    const int glyphCount;
383    const float x;
384    const float y;
385};
386
387struct TextOnPathOp : RecordedOp {
388    TextOnPathOp(BASE_PARAMS, const glyph_t* glyphs, int glyphCount,
389            const SkPath* path, float hOffset, float vOffset)
390            : SUPER(TextOnPathOp)
391            , glyphs(glyphs)
392            , glyphCount(glyphCount)
393            , path(path)
394            , hOffset(hOffset)
395            , vOffset(vOffset) {}
396    const glyph_t* glyphs;
397    const int glyphCount;
398
399    const SkPath* path;
400    const float hOffset;
401    const float vOffset;
402};
403
404struct TextureLayerOp : RecordedOp {
405    TextureLayerOp(BASE_PARAMS_PAINTLESS, Layer* layer)
406            : SUPER_PAINTLESS(TextureLayerOp)
407            , layer(layer) {}
408    Layer* layer;
409};
410
411////////////////////////////////////////////////////////////////////////////////////////////////////
412// Layers
413////////////////////////////////////////////////////////////////////////////////////////////////////
414
415/**
416 * Stateful operation! denotes the creation of an off-screen layer,
417 * and that commands following will render into it.
418 */
419struct BeginLayerOp : RecordedOp {
420    BeginLayerOp(BASE_PARAMS)
421            : SUPER(BeginLayerOp) {}
422};
423
424/**
425 * Stateful operation! Denotes end of off-screen layer, and that
426 * commands since last BeginLayerOp should be drawn into parent FBO.
427 *
428 * State in this op is empty, it just serves to signal that a layer has been finished.
429 */
430struct EndLayerOp : RecordedOp {
431    EndLayerOp()
432            : RecordedOp(RecordedOpId::EndLayerOp, Rect(), Matrix4::identity(), nullptr, nullptr) {}
433};
434
435struct BeginUnclippedLayerOp : RecordedOp {
436    BeginUnclippedLayerOp(BASE_PARAMS)
437            : SUPER(BeginUnclippedLayerOp) {}
438};
439
440struct EndUnclippedLayerOp : RecordedOp {
441    EndUnclippedLayerOp()
442            : RecordedOp(RecordedOpId::EndUnclippedLayerOp, Rect(), Matrix4::identity(), nullptr, nullptr) {}
443};
444
445struct CopyToLayerOp : RecordedOp {
446    CopyToLayerOp(const RecordedOp& op, OffscreenBuffer** layerHandle)
447            : RecordedOp(RecordedOpId::CopyToLayerOp,
448                    op.unmappedBounds,
449                    op.localMatrix,
450                    nullptr, // clip intentionally ignored
451                    op.paint)
452            , layerHandle(layerHandle) {}
453
454    // Records a handle to the Layer object, since the Layer itself won't be
455    // constructed until after this operation is constructed.
456    OffscreenBuffer** layerHandle;
457};
458
459
460// draw the parameter layer underneath
461struct CopyFromLayerOp : RecordedOp {
462    CopyFromLayerOp(const RecordedOp& op, OffscreenBuffer** layerHandle)
463            : RecordedOp(RecordedOpId::CopyFromLayerOp,
464                    op.unmappedBounds,
465                    op.localMatrix,
466                    nullptr, // clip intentionally ignored
467                    op.paint)
468            , layerHandle(layerHandle) {}
469
470    // Records a handle to the Layer object, since the Layer itself won't be
471    // constructed until after this operation is constructed.
472    OffscreenBuffer** layerHandle;
473};
474
475/**
476 * Draws an OffscreenBuffer.
477 *
478 * Alpha, mode, and colorfilter are embedded, since LayerOps are always dynamically generated,
479 * when creating/tracking a SkPaint* during defer isn't worth the bother.
480 */
481struct LayerOp : RecordedOp {
482    // Records a one-use (saveLayer) layer for drawing. Once drawn, the layer will be destroyed.
483    LayerOp(BASE_PARAMS, OffscreenBuffer** layerHandle)
484            : SUPER_PAINTLESS(LayerOp)
485            , layerHandle(layerHandle)
486            , alpha(paint ? paint->getAlpha() / 255.0f : 1.0f)
487            , mode(PaintUtils::getXfermodeDirect(paint))
488            , colorFilter(paint ? paint->getColorFilter() : nullptr)
489            , destroy(true) {}
490
491    LayerOp(RenderNode& node)
492            : RecordedOp(RecordedOpId::LayerOp, Rect(node.getWidth(), node.getHeight()), Matrix4::identity(), nullptr, nullptr)
493            , layerHandle(node.getLayerHandle())
494            , alpha(node.properties().layerProperties().alpha() / 255.0f)
495            , mode(node.properties().layerProperties().xferMode())
496            , colorFilter(node.properties().layerProperties().colorFilter())
497            , destroy(false) {}
498
499    // Records a handle to the Layer object, since the Layer itself won't be
500    // constructed until after this operation is constructed.
501    OffscreenBuffer** layerHandle;
502    const float alpha;
503    const SkXfermode::Mode mode;
504
505    // pointer to object owned by either LayerProperties, or a recorded Paint object in a
506    // BeginLayerOp. Lives longer than LayerOp in either case, so no skia ref counting is used.
507    SkColorFilter* colorFilter;
508
509    // whether to destroy the layer, once rendered
510    const bool destroy;
511};
512
513}; // namespace uirenderer
514}; // namespace android
515
516#endif // ANDROID_HWUI_RECORDED_OP_H
517