Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame^] | 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_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 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | namespace 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 | /** |
| 41 | * Holds the resolved clip, transform, and bounds of a recordedOp, when replayed with a snapshot |
| 42 | */ |
| 43 | class ResolvedRenderState { |
| 44 | public: |
| 45 | // TODO: remove the mapRects/matrix multiply when snapshot & recorded transforms are translates |
| 46 | ResolvedRenderState(const Snapshot& snapshot, const RecordedOp& recordedOp) { |
| 47 | /* TODO: benchmark a fast path for translate-only matrices, such as: |
| 48 | if (CC_LIKELY(snapshot.transform->getType() == Matrix4::kTypeTranslate |
| 49 | && recordedOp.localMatrix.getType() == Matrix4::kTypeTranslate)) { |
| 50 | float translateX = snapshot.transform->getTranslateX() + recordedOp.localMatrix.getTranslateX(); |
| 51 | float translateY = snapshot.transform->getTranslateY() + recordedOp.localMatrix.getTranslateY(); |
| 52 | transform.loadTranslate(translateX, translateY, 0); |
| 53 | |
| 54 | // resolvedClipRect = intersect(parentMatrix * localClip, parentClip) |
| 55 | clipRect = recordedOp.localClipRect; |
| 56 | clipRect.translate(translateX, translateY); |
| 57 | clipRect.doIntersect(snapshot.getClipRect()); |
| 58 | clipRect.snapToPixelBoundaries(); |
| 59 | |
| 60 | // resolvedClippedBounds = intersect(resolvedMatrix * opBounds, resolvedClipRect) |
| 61 | clippedBounds = recordedOp.unmappedBounds; |
| 62 | clippedBounds.translate(translateX, translateY); |
| 63 | } ... */ |
| 64 | |
| 65 | // resolvedMatrix = parentMatrix * localMatrix |
| 66 | transform.loadMultiply(*snapshot.transform, recordedOp.localMatrix); |
| 67 | |
| 68 | // resolvedClipRect = intersect(parentMatrix * localClip, parentClip) |
| 69 | clipRect = recordedOp.localClipRect; |
| 70 | snapshot.transform->mapRect(clipRect); |
| 71 | clipRect.doIntersect(snapshot.getClipRect()); |
| 72 | clipRect.snapToPixelBoundaries(); |
| 73 | |
| 74 | // resolvedClippedBounds = intersect(resolvedMatrix * opBounds, resolvedClipRect) |
| 75 | clippedBounds = recordedOp.unmappedBounds; |
| 76 | transform.mapRect(clippedBounds); |
| 77 | |
| 78 | if (clipRect.left > clippedBounds.left) clipSideFlags |= OpClipSideFlags::Left; |
| 79 | if (clipRect.top > clippedBounds.top) clipSideFlags |= OpClipSideFlags::Top; |
| 80 | if (clipRect.right < clippedBounds.right) clipSideFlags |= OpClipSideFlags::Right; |
| 81 | if (clipRect.bottom < clippedBounds.bottom) clipSideFlags |= OpClipSideFlags::Bottom; |
| 82 | clippedBounds.doIntersect(clipRect); |
| 83 | |
| 84 | /** |
| 85 | * TODO: once we support complex clips, we may want to reject to avoid that work where |
| 86 | * possible. Should we: |
| 87 | * 1 - quickreject based on clippedBounds, quick early (duplicating logic in resolvedOp) |
| 88 | * 2 - merge stuff into tryConstruct factory method, so it can handle quickRejection |
| 89 | * and early return null in one place. |
| 90 | */ |
| 91 | } |
| 92 | Matrix4 transform; |
| 93 | Rect clipRect; |
| 94 | int clipSideFlags = 0; |
| 95 | Rect clippedBounds; |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * Self-contained op wrapper, containing all resolved state required to draw the op. |
| 100 | * |
| 101 | * Stashed pointers within all point to longer lived objects, with no ownership implied. |
| 102 | */ |
| 103 | class BakedOpState { |
| 104 | public: |
| 105 | static BakedOpState* tryConstruct(LinearAllocator& allocator, |
| 106 | const Snapshot& snapshot, const RecordedOp& recordedOp) { |
| 107 | BakedOpState* bakedOp = new (allocator) BakedOpState( |
| 108 | snapshot, recordedOp); |
| 109 | if (bakedOp->computedState.clippedBounds.isEmpty()) { |
| 110 | // bounds are empty, so op is rejected |
| 111 | allocator.rewindIfLastAlloc(bakedOp); |
| 112 | return nullptr; |
| 113 | } |
| 114 | return bakedOp; |
| 115 | } |
| 116 | |
| 117 | static void* operator new(size_t size, LinearAllocator& allocator) { |
| 118 | return allocator.alloc(size); |
| 119 | } |
| 120 | |
| 121 | // computed state: |
| 122 | const ResolvedRenderState computedState; |
| 123 | |
| 124 | // simple state (straight pointer/value storage): |
| 125 | const float alpha; |
| 126 | const RoundRectClipState* roundRectClipState; |
| 127 | const ProjectionPathMask* projectionPathMask; |
| 128 | const RecordedOp* op; |
| 129 | |
| 130 | private: |
| 131 | BakedOpState(const Snapshot& snapshot, const RecordedOp& recordedOp) |
| 132 | : computedState(snapshot, recordedOp) |
| 133 | , alpha(snapshot.alpha) |
| 134 | , roundRectClipState(snapshot.roundRectClipState) |
| 135 | , projectionPathMask(snapshot.projectionPathMask) |
| 136 | , op(&recordedOp) {} |
| 137 | }; |
| 138 | |
| 139 | }; // namespace uirenderer |
| 140 | }; // namespace android |
| 141 | |
| 142 | #endif // ANDROID_HWUI_BAKED_OP_STATE_H |