blob: 9df4e3aa5442b20227bdc1d7e4d54123d65fec73 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
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_BAKED_OP_STATE_H
18#define ANDROID_HWUI_BAKED_OP_STATE_H
19
20#include "Matrix.h"
21#include "RecordedOp.h"
22#include "Rect.h"
23#include "Snapshot.h"
24
25namespace android {
26namespace uirenderer {
27
28namespace OpClipSideFlags {
29 enum {
30 None = 0x0,
31 Left = 0x1,
32 Top = 0x2,
33 Right = 0x4,
34 Bottom = 0x8,
35 Full = 0xF,
36 // ConservativeFull = 0x1F needed?
37 };
38}
39
40/**
Chris Craik15c3f192015-12-03 12:16:56 -080041 * Holds a list of BakedOpStates of ops that can be drawn together
42 */
43struct MergedBakedOpList {
44 const BakedOpState*const* states;
45 size_t count;
46 int clipSideFlags;
47 Rect clip;
48};
49
50/**
Chris Craikb565df12015-10-05 13:00:52 -070051 * Holds the resolved clip, transform, and bounds of a recordedOp, when replayed with a snapshot
52 */
53class ResolvedRenderState {
54public:
Chris Craike4db79d2015-12-22 16:32:23 -080055 ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot,
56 const RecordedOp& recordedOp, bool expandForStroke);
Chris Craikb565df12015-10-05 13:00:52 -070057
Chris Craike4db79d2015-12-22 16:32:23 -080058 // Constructor for unbounded ops without transform/clip (namely shadows)
59 ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot);
Chris Craikd3daa312015-11-06 10:59:56 -080060
Chris Craikd7448e62015-12-15 10:34:36 -080061 Rect computeLocalSpaceClip() const {
62 Matrix4 inverse;
63 inverse.loadInverse(transform);
64
Chris Craike4db79d2015-12-22 16:32:23 -080065 Rect outClip(clipRect());
Chris Craikd7448e62015-12-15 10:34:36 -080066 inverse.mapRect(outClip);
67 return outClip;
68 }
69
Chris Craikb565df12015-10-05 13:00:52 -070070 Matrix4 transform;
Chris Craike4db79d2015-12-22 16:32:23 -080071 const Rect& clipRect() const {
72 return clipState->rect;
73 }
74 bool requiresClip() const {
75 return clipSideFlags != OpClipSideFlags::None
76 || CC_UNLIKELY(clipState->mode != ClipMode::Rectangle);
77 }
78
79 // returns the clip if it's needed to draw the operation, otherwise nullptr
80 const ClipBase* getClipIfNeeded() const {
81 return requiresClip() ? clipState : nullptr;
82 }
83 const ClipBase* clipState = nullptr;
Chris Craikb565df12015-10-05 13:00:52 -070084 int clipSideFlags = 0;
85 Rect clippedBounds;
86};
87
88/**
89 * Self-contained op wrapper, containing all resolved state required to draw the op.
90 *
91 * Stashed pointers within all point to longer lived objects, with no ownership implied.
92 */
93class BakedOpState {
94public:
95 static BakedOpState* tryConstruct(LinearAllocator& allocator,
Chris Craike4db79d2015-12-22 16:32:23 -080096 Snapshot& snapshot, const RecordedOp& recordedOp) {
97 BakedOpState* bakedState = new (allocator) BakedOpState(
98 allocator, snapshot, recordedOp, false);
Chris Craik386aa032015-12-07 17:08:25 -080099 if (bakedState->computedState.clippedBounds.isEmpty()) {
Chris Craikb565df12015-10-05 13:00:52 -0700100 // bounds are empty, so op is rejected
Chris Craik386aa032015-12-07 17:08:25 -0800101 allocator.rewindIfLastAlloc(bakedState);
Chris Craikb565df12015-10-05 13:00:52 -0700102 return nullptr;
103 }
Chris Craik386aa032015-12-07 17:08:25 -0800104 return bakedState;
105 }
106
107 enum class StrokeBehavior {
108 // stroking is forced, regardless of style on paint
109 Forced,
110 // stroking is defined by style on paint
111 StyleDefined,
112 };
113
114 static BakedOpState* tryStrokeableOpConstruct(LinearAllocator& allocator,
Chris Craike4db79d2015-12-22 16:32:23 -0800115 Snapshot& snapshot, const RecordedOp& recordedOp, StrokeBehavior strokeBehavior) {
Chris Craik386aa032015-12-07 17:08:25 -0800116 bool expandForStroke = (strokeBehavior == StrokeBehavior::StyleDefined)
117 ? (recordedOp.paint && recordedOp.paint->getStyle() != SkPaint::kFill_Style)
118 : true;
119
120 BakedOpState* bakedState = new (allocator) BakedOpState(
Chris Craike4db79d2015-12-22 16:32:23 -0800121 allocator, snapshot, recordedOp, expandForStroke);
Chris Craik386aa032015-12-07 17:08:25 -0800122 if (bakedState->computedState.clippedBounds.isEmpty()) {
123 // bounds are empty, so op is rejected
124 allocator.rewindIfLastAlloc(bakedState);
125 return nullptr;
126 }
127 return bakedState;
Chris Craikb565df12015-10-05 13:00:52 -0700128 }
129
Chris Craikd3daa312015-11-06 10:59:56 -0800130 static BakedOpState* tryShadowOpConstruct(LinearAllocator& allocator,
Chris Craike4db79d2015-12-22 16:32:23 -0800131 Snapshot& snapshot, const ShadowOp* shadowOpPtr) {
Chris Craikd3daa312015-11-06 10:59:56 -0800132 if (snapshot.getRenderTargetClip().isEmpty()) return nullptr;
133
134 // clip isn't empty, so construct the op
Chris Craike4db79d2015-12-22 16:32:23 -0800135 return new (allocator) BakedOpState(allocator, snapshot, shadowOpPtr);
Chris Craikd3daa312015-11-06 10:59:56 -0800136 }
137
Chris Craikb565df12015-10-05 13:00:52 -0700138 static void* operator new(size_t size, LinearAllocator& allocator) {
139 return allocator.alloc(size);
140 }
141
142 // computed state:
143 const ResolvedRenderState computedState;
144
145 // simple state (straight pointer/value storage):
146 const float alpha;
147 const RoundRectClipState* roundRectClipState;
148 const ProjectionPathMask* projectionPathMask;
149 const RecordedOp* op;
150
151private:
Chris Craike4db79d2015-12-22 16:32:23 -0800152 BakedOpState(LinearAllocator& allocator, Snapshot& snapshot,
153 const RecordedOp& recordedOp, bool expandForStroke)
154 : computedState(allocator, snapshot, recordedOp, expandForStroke)
Chris Craikb565df12015-10-05 13:00:52 -0700155 , alpha(snapshot.alpha)
156 , roundRectClipState(snapshot.roundRectClipState)
157 , projectionPathMask(snapshot.projectionPathMask)
158 , op(&recordedOp) {}
Chris Craikd3daa312015-11-06 10:59:56 -0800159
Chris Craike4db79d2015-12-22 16:32:23 -0800160 BakedOpState(LinearAllocator& allocator, Snapshot& snapshot, const ShadowOp* shadowOpPtr)
161 : computedState(allocator, snapshot)
Chris Craikd3daa312015-11-06 10:59:56 -0800162 , alpha(snapshot.alpha)
163 , roundRectClipState(snapshot.roundRectClipState)
164 , projectionPathMask(snapshot.projectionPathMask)
165 , op(shadowOpPtr) {}
Chris Craikb565df12015-10-05 13:00:52 -0700166};
167
168}; // namespace uirenderer
169}; // namespace android
170
171#endif // ANDROID_HWUI_BAKED_OP_STATE_H