RecordedOp.h revision 4c3980b6e43cc7c0541f54b8e7e2d9d6108be622
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    // Note: undefined record-time bounds, since this op fills the clip
261    // TODO: explicitly define bounds
262    FunctorOp(const Matrix4& localMatrix, const ClipBase* localClip, Functor* functor)
263            : RecordedOp(RecordedOpId::FunctorOp, Rect(), localMatrix, localClip, nullptr)
264            , functor(functor) {}
265    Functor* functor;
266};
267
268struct LinesOp : RecordedOp {
269    LinesOp(BASE_PARAMS, const float* points, const int floatCount)
270            : SUPER(LinesOp)
271            , points(points)
272            , floatCount(floatCount) {}
273    const float* points;
274    const int floatCount;
275};
276
277struct OvalOp : RecordedOp {
278    OvalOp(BASE_PARAMS)
279            : SUPER(OvalOp) {}
280};
281
282struct PatchOp : RecordedOp {
283    PatchOp(BASE_PARAMS, const SkBitmap* bitmap, const Res_png_9patch* patch)
284            : SUPER(PatchOp)
285            , bitmap(bitmap)
286            , patch(patch) {}
287    const SkBitmap* bitmap;
288    const Res_png_9patch* patch;
289};
290
291struct PathOp : RecordedOp {
292    PathOp(BASE_PARAMS, const SkPath* path)
293            : SUPER(PathOp)
294            , path(path) {}
295    const SkPath* path;
296};
297
298struct PointsOp : RecordedOp {
299    PointsOp(BASE_PARAMS, const float* points, const int floatCount)
300            : SUPER(PointsOp)
301            , points(points)
302            , floatCount(floatCount) {}
303    const float* points;
304    const int floatCount;
305};
306
307struct RectOp : RecordedOp {
308    RectOp(BASE_PARAMS)
309            : SUPER(RectOp) {}
310};
311
312struct RoundRectOp : RecordedOp {
313    RoundRectOp(BASE_PARAMS, float rx, float ry)
314            : SUPER(RoundRectOp)
315            , rx(rx)
316            , ry(ry) {}
317    const float rx;
318    const float ry;
319};
320
321struct RoundRectPropsOp : RecordedOp {
322    RoundRectPropsOp(const Matrix4& localMatrix, const ClipBase* localClip, const SkPaint* paint,
323            float* left, float* top, float* right, float* bottom, float *rx, float *ry)
324            : RecordedOp(RecordedOpId::RoundRectPropsOp, Rect(), localMatrix, localClip, paint)
325            , left(left)
326            , top(top)
327            , right(right)
328            , bottom(bottom)
329            , rx(rx)
330            , ry(ry) {}
331    const float* left;
332    const float* top;
333    const float* right;
334    const float* bottom;
335    const float* rx;
336    const float* ry;
337};
338
339struct VectorDrawableOp : RecordedOp {
340    VectorDrawableOp(VectorDrawable::Tree* tree, BASE_PARAMS_PAINTLESS)
341            : SUPER_PAINTLESS(VectorDrawableOp)
342            , vectorDrawable(tree) {}
343    VectorDrawable::Tree* vectorDrawable;
344};
345
346/**
347 * Real-time, dynamic-lit shadow.
348 *
349 * Uses invalid/empty bounds and matrix since ShadowOp bounds aren't known at defer time,
350 * and are resolved dynamically, and transform isn't needed.
351 *
352 * State construction handles these properties specially, ignoring matrix/bounds.
353 */
354struct ShadowOp : RecordedOp {
355    ShadowOp(sp<TessellationCache::ShadowTask>& shadowTask, float casterAlpha)
356            : RecordedOp(RecordedOpId::ShadowOp, Rect(), Matrix4::identity(), nullptr, nullptr)
357            , shadowTask(shadowTask)
358            , casterAlpha(casterAlpha) {
359    };
360    sp<TessellationCache::ShadowTask> shadowTask;
361    const float casterAlpha;
362};
363
364struct SimpleRectsOp : RecordedOp { // Filled, no AA (TODO: better name?)
365    SimpleRectsOp(BASE_PARAMS, Vertex* vertices, size_t vertexCount)
366            : SUPER(SimpleRectsOp)
367            , vertices(vertices)
368            , vertexCount(vertexCount) {}
369    Vertex* vertices;
370    const size_t vertexCount;
371};
372
373struct TextOp : RecordedOp {
374    TextOp(BASE_PARAMS, const glyph_t* glyphs, const float* positions, int glyphCount,
375            float x, float y)
376            : SUPER(TextOp)
377            , glyphs(glyphs)
378            , positions(positions)
379            , glyphCount(glyphCount)
380            , x(x)
381            , y(y) {}
382    const glyph_t* glyphs;
383    const float* positions;
384    const int glyphCount;
385    const float x;
386    const float y;
387};
388
389struct TextOnPathOp : RecordedOp {
390    // TODO: explicitly define bounds
391    TextOnPathOp(const Matrix4& localMatrix, const ClipBase* localClip, const SkPaint* paint,
392            const glyph_t* glyphs, int glyphCount, const SkPath* path, float hOffset, float vOffset)
393            : RecordedOp(RecordedOpId::TextOnPathOp, Rect(), localMatrix, localClip, paint)
394            , glyphs(glyphs)
395            , glyphCount(glyphCount)
396            , path(path)
397            , hOffset(hOffset)
398            , vOffset(vOffset) {}
399    const glyph_t* glyphs;
400    const int glyphCount;
401
402    const SkPath* path;
403    const float hOffset;
404    const float vOffset;
405};
406
407struct TextureLayerOp : RecordedOp {
408    TextureLayerOp(BASE_PARAMS_PAINTLESS, Layer* layer)
409            : SUPER_PAINTLESS(TextureLayerOp)
410            , layer(layer) {}
411    Layer* layer;
412};
413
414////////////////////////////////////////////////////////////////////////////////////////////////////
415// Layers
416////////////////////////////////////////////////////////////////////////////////////////////////////
417
418/**
419 * Stateful operation! denotes the creation of an off-screen layer,
420 * and that commands following will render into it.
421 */
422struct BeginLayerOp : RecordedOp {
423    BeginLayerOp(BASE_PARAMS)
424            : SUPER(BeginLayerOp) {}
425};
426
427/**
428 * Stateful operation! Denotes end of off-screen layer, and that
429 * commands since last BeginLayerOp should be drawn into parent FBO.
430 *
431 * State in this op is empty, it just serves to signal that a layer has been finished.
432 */
433struct EndLayerOp : RecordedOp {
434    EndLayerOp()
435            : RecordedOp(RecordedOpId::EndLayerOp, Rect(), Matrix4::identity(), nullptr, nullptr) {}
436};
437
438struct BeginUnclippedLayerOp : RecordedOp {
439    BeginUnclippedLayerOp(BASE_PARAMS)
440            : SUPER(BeginUnclippedLayerOp) {}
441};
442
443struct EndUnclippedLayerOp : RecordedOp {
444    EndUnclippedLayerOp()
445            : RecordedOp(RecordedOpId::EndUnclippedLayerOp, Rect(), Matrix4::identity(), nullptr, nullptr) {}
446};
447
448struct CopyToLayerOp : RecordedOp {
449    CopyToLayerOp(const RecordedOp& op, OffscreenBuffer** layerHandle)
450            : RecordedOp(RecordedOpId::CopyToLayerOp,
451                    op.unmappedBounds,
452                    op.localMatrix,
453                    nullptr, // clip intentionally ignored
454                    op.paint)
455            , layerHandle(layerHandle) {}
456
457    // Records a handle to the Layer object, since the Layer itself won't be
458    // constructed until after this operation is constructed.
459    OffscreenBuffer** layerHandle;
460};
461
462
463// draw the parameter layer underneath
464struct CopyFromLayerOp : RecordedOp {
465    CopyFromLayerOp(const RecordedOp& op, OffscreenBuffer** layerHandle)
466            : RecordedOp(RecordedOpId::CopyFromLayerOp,
467                    op.unmappedBounds,
468                    op.localMatrix,
469                    nullptr, // clip intentionally ignored
470                    op.paint)
471            , layerHandle(layerHandle) {}
472
473    // Records a handle to the Layer object, since the Layer itself won't be
474    // constructed until after this operation is constructed.
475    OffscreenBuffer** layerHandle;
476};
477
478/**
479 * Draws an OffscreenBuffer.
480 *
481 * Alpha, mode, and colorfilter are embedded, since LayerOps are always dynamically generated,
482 * when creating/tracking a SkPaint* during defer isn't worth the bother.
483 */
484struct LayerOp : RecordedOp {
485    // Records a one-use (saveLayer) layer for drawing. Once drawn, the layer will be destroyed.
486    LayerOp(BASE_PARAMS, OffscreenBuffer** layerHandle)
487            : SUPER_PAINTLESS(LayerOp)
488            , layerHandle(layerHandle)
489            , alpha(paint ? paint->getAlpha() / 255.0f : 1.0f)
490            , mode(PaintUtils::getXfermodeDirect(paint))
491            , colorFilter(paint ? paint->getColorFilter() : nullptr)
492            , destroy(true) {}
493
494    LayerOp(RenderNode& node)
495            : RecordedOp(RecordedOpId::LayerOp, Rect(node.getWidth(), node.getHeight()), Matrix4::identity(), nullptr, nullptr)
496            , layerHandle(node.getLayerHandle())
497            , alpha(node.properties().layerProperties().alpha() / 255.0f)
498            , mode(node.properties().layerProperties().xferMode())
499            , colorFilter(node.properties().layerProperties().colorFilter())
500            , destroy(false) {}
501
502    // Records a handle to the Layer object, since the Layer itself won't be
503    // constructed until after this operation is constructed.
504    OffscreenBuffer** layerHandle;
505    const float alpha;
506    const SkXfermode::Mode mode;
507
508    // pointer to object owned by either LayerProperties, or a recorded Paint object in a
509    // BeginLayerOp. Lives longer than LayerOp in either case, so no skia ref counting is used.
510    SkColorFilter* colorFilter;
511
512    // whether to destroy the layer, once rendered
513    const bool destroy;
514};
515
516}; // namespace uirenderer
517}; // namespace android
518
519#endif // ANDROID_HWUI_RECORDED_OP_H
520