blob: 5c7b43f5a034f780e97fdaab6bbe837abfd5654f [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 Craik7435eb12016-01-07 17:41:40 -080061 // Constructor for primitive ops provided clip, and no transform
62 ResolvedRenderState(const ClipRect* viewportRect, const Rect& dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -080063
Chris Craikd7448e62015-12-15 10:34:36 -080064 Rect computeLocalSpaceClip() const {
65 Matrix4 inverse;
66 inverse.loadInverse(transform);
67
Chris Craike4db79d2015-12-22 16:32:23 -080068 Rect outClip(clipRect());
Chris Craikd7448e62015-12-15 10:34:36 -080069 inverse.mapRect(outClip);
70 return outClip;
71 }
72
Chris Craike4db79d2015-12-22 16:32:23 -080073 const Rect& clipRect() const {
74 return clipState->rect;
75 }
Chris Craikb87eadd2016-01-06 09:16:05 -080076
Chris Craike4db79d2015-12-22 16:32:23 -080077 bool requiresClip() const {
78 return clipSideFlags != OpClipSideFlags::None
Chris Craik7435eb12016-01-07 17:41:40 -080079 || CC_UNLIKELY(clipState->mode != ClipMode::Rectangle);
Chris Craike4db79d2015-12-22 16:32:23 -080080 }
81
82 // returns the clip if it's needed to draw the operation, otherwise nullptr
83 const ClipBase* getClipIfNeeded() const {
84 return requiresClip() ? clipState : nullptr;
85 }
Chris Craikb87eadd2016-01-06 09:16:05 -080086
87 Matrix4 transform;
Chris Craike4db79d2015-12-22 16:32:23 -080088 const ClipBase* clipState = nullptr;
Chris Craikb565df12015-10-05 13:00:52 -070089 Rect clippedBounds;
Chris Craikb87eadd2016-01-06 09:16:05 -080090 int clipSideFlags = 0;
Chris Craikb565df12015-10-05 13:00:52 -070091};
92
93/**
94 * Self-contained op wrapper, containing all resolved state required to draw the op.
95 *
96 * Stashed pointers within all point to longer lived objects, with no ownership implied.
97 */
98class BakedOpState {
99public:
100 static BakedOpState* tryConstruct(LinearAllocator& allocator,
Chris Craike4db79d2015-12-22 16:32:23 -0800101 Snapshot& snapshot, const RecordedOp& recordedOp) {
102 BakedOpState* bakedState = new (allocator) BakedOpState(
103 allocator, snapshot, recordedOp, false);
Chris Craik386aa032015-12-07 17:08:25 -0800104 if (bakedState->computedState.clippedBounds.isEmpty()) {
Chris Craikb565df12015-10-05 13:00:52 -0700105 // bounds are empty, so op is rejected
Chris Craik386aa032015-12-07 17:08:25 -0800106 allocator.rewindIfLastAlloc(bakedState);
Chris Craikb565df12015-10-05 13:00:52 -0700107 return nullptr;
108 }
Chris Craik386aa032015-12-07 17:08:25 -0800109 return bakedState;
110 }
111
112 enum class StrokeBehavior {
113 // stroking is forced, regardless of style on paint
114 Forced,
115 // stroking is defined by style on paint
116 StyleDefined,
117 };
118
119 static BakedOpState* tryStrokeableOpConstruct(LinearAllocator& allocator,
Chris Craike4db79d2015-12-22 16:32:23 -0800120 Snapshot& snapshot, const RecordedOp& recordedOp, StrokeBehavior strokeBehavior) {
Chris Craik386aa032015-12-07 17:08:25 -0800121 bool expandForStroke = (strokeBehavior == StrokeBehavior::StyleDefined)
122 ? (recordedOp.paint && recordedOp.paint->getStyle() != SkPaint::kFill_Style)
123 : true;
124
125 BakedOpState* bakedState = new (allocator) BakedOpState(
Chris Craike4db79d2015-12-22 16:32:23 -0800126 allocator, snapshot, recordedOp, expandForStroke);
Chris Craik386aa032015-12-07 17:08:25 -0800127 if (bakedState->computedState.clippedBounds.isEmpty()) {
128 // bounds are empty, so op is rejected
129 allocator.rewindIfLastAlloc(bakedState);
130 return nullptr;
131 }
132 return bakedState;
Chris Craikb565df12015-10-05 13:00:52 -0700133 }
134
Chris Craikd3daa312015-11-06 10:59:56 -0800135 static BakedOpState* tryShadowOpConstruct(LinearAllocator& allocator,
Chris Craike4db79d2015-12-22 16:32:23 -0800136 Snapshot& snapshot, const ShadowOp* shadowOpPtr) {
Chris Craikd3daa312015-11-06 10:59:56 -0800137 if (snapshot.getRenderTargetClip().isEmpty()) return nullptr;
138
139 // clip isn't empty, so construct the op
Chris Craike4db79d2015-12-22 16:32:23 -0800140 return new (allocator) BakedOpState(allocator, snapshot, shadowOpPtr);
Chris Craikd3daa312015-11-06 10:59:56 -0800141 }
142
Chris Craikb87eadd2016-01-06 09:16:05 -0800143 static BakedOpState* directConstruct(LinearAllocator& allocator,
Chris Craik7435eb12016-01-07 17:41:40 -0800144 const ClipRect* clip, const Rect& dstRect, const RecordedOp& recordedOp) {
145 return new (allocator) BakedOpState(clip, dstRect, recordedOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800146 }
147
Chris Craikb565df12015-10-05 13:00:52 -0700148 static void* operator new(size_t size, LinearAllocator& allocator) {
149 return allocator.alloc(size);
150 }
151
152 // computed state:
Chris Craikb87eadd2016-01-06 09:16:05 -0800153 ResolvedRenderState computedState;
Chris Craikb565df12015-10-05 13:00:52 -0700154
155 // simple state (straight pointer/value storage):
156 const float alpha;
157 const RoundRectClipState* roundRectClipState;
158 const ProjectionPathMask* projectionPathMask;
159 const RecordedOp* op;
160
161private:
Chris Craike4db79d2015-12-22 16:32:23 -0800162 BakedOpState(LinearAllocator& allocator, Snapshot& snapshot,
163 const RecordedOp& recordedOp, bool expandForStroke)
164 : computedState(allocator, snapshot, recordedOp, expandForStroke)
Chris Craikb565df12015-10-05 13:00:52 -0700165 , alpha(snapshot.alpha)
166 , roundRectClipState(snapshot.roundRectClipState)
167 , projectionPathMask(snapshot.projectionPathMask)
168 , op(&recordedOp) {}
Chris Craikd3daa312015-11-06 10:59:56 -0800169
Chris Craike4db79d2015-12-22 16:32:23 -0800170 BakedOpState(LinearAllocator& allocator, Snapshot& snapshot, const ShadowOp* shadowOpPtr)
171 : computedState(allocator, snapshot)
Chris Craikd3daa312015-11-06 10:59:56 -0800172 , alpha(snapshot.alpha)
173 , roundRectClipState(snapshot.roundRectClipState)
174 , projectionPathMask(snapshot.projectionPathMask)
175 , op(shadowOpPtr) {}
Chris Craikb87eadd2016-01-06 09:16:05 -0800176
Chris Craik7435eb12016-01-07 17:41:40 -0800177 BakedOpState(const ClipRect* viewportRect, const Rect& dstRect, const RecordedOp& recordedOp)
178 : computedState(viewportRect, dstRect)
Chris Craikb87eadd2016-01-06 09:16:05 -0800179 , alpha(1.0f)
180 , roundRectClipState(nullptr)
181 , projectionPathMask(nullptr)
182 , op(&recordedOp) {}
Chris Craikb565df12015-10-05 13:00:52 -0700183};
184
185}; // namespace uirenderer
186}; // namespace android
187
188#endif // ANDROID_HWUI_BAKED_OP_STATE_H