2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_SNAPSHOT_H
18 #define ANDROID_HWUI_SNAPSHOT_H
20 #include <GLES2/gl2.h>
21 #include <GLES2/gl2ext.h>
23 #include <utils/LinearAllocator.h>
24 #include <utils/RefBase.h>
25 #include <ui/Region.h>
34 #include "utils/Macros.h"
37 namespace uirenderer {
40 * Temporary structure holding information for a single outline clip.
42 * These structures are treated as immutable once created, and only exist for a single frame, which
43 * is why they may only be allocated with a LinearAllocator.
45 class RoundRectClipState {
47 /** static void* operator new(size_t size); PURPOSELY OMITTED, allocator only **/
48 static void* operator new(size_t size, LinearAllocator& allocator) {
49 return allocator.alloc(size);
52 bool areaRequiresRoundRectClip(const Rect& rect) const {
53 return rect.intersects(dangerRects[0])
54 || rect.intersects(dangerRects[1])
55 || rect.intersects(dangerRects[2])
56 || rect.intersects(dangerRects[3]);
66 class ProjectionPathMask {
68 /** static void* operator new(size_t size); PURPOSELY OMITTED, allocator only **/
69 static void* operator new(size_t size, LinearAllocator& allocator) {
70 return allocator.alloc(size);
73 const SkPath* projectionMask;
74 Matrix4 projectionMaskTransform;
78 * A snapshot holds information about the current state of the rendering
79 * surface. A snapshot is usually created whenever the user calls save()
80 * and discarded when the user calls restore(). Once a snapshot is created,
81 * it can hold information for deferred rendering.
83 * Each snapshot has a link to a previous snapshot, indicating the previous
84 * state of the renderer.
86 class Snapshot: public LightRefBase<Snapshot> {
90 Snapshot(const sp<Snapshot>& s, int saveFlags);
93 * Various flags set on ::flags.
97 * Indicates that the clip region was modified. When this
98 * snapshot is restored so must the clip.
102 * Indicates that this snapshot was created when saving
107 * Indicates that this snapshot is a special type of layer
108 * backed by an FBO. This flag only makes sense when the
109 * flag kFlagIsLayer is also set.
111 * Viewport has been modified to fit the new Fbo, and must be
112 * restored when this snapshot is restored.
114 kFlagIsFboLayer = 0x4,
116 * Indicates that this snapshot or an ancestor snapshot is
119 kFlagFboTarget = 0x8,
123 * Modifies the current clip with the new clip rectangle and
124 * the specified operation. The specified rectangle is transformed
125 * by this snapshot's trasnformation.
127 bool clip(float left, float top, float right, float bottom,
128 SkRegion::Op op = SkRegion::kIntersect_Op);
131 * Modifies the current clip with the new clip rectangle and
132 * the specified operation. The specified rectangle is considered
133 * already transformed.
135 bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
138 * Modifies the current clip with the specified region and operation.
139 * The specified region is considered already transformed.
141 bool clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
144 * Modifies the current clip with the specified path and operation.
146 bool clipPath(const SkPath& path, SkRegion::Op op);
149 * Sets the current clip.
151 void setClip(float left, float top, float right, float bottom);
154 * Returns the current clip in local coordinates. The clip rect is
155 * transformed by the inverse transform matrix.
157 ANDROID_API const Rect& getLocalClip();
160 * Returns the current clip in render target coordinates.
162 const Rect& getRenderTargetClip() { return mClipArea->getClipRect(); }
165 * Accessor functions so that the clip area can stay private
167 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
168 const Rect& getClipRect() const { return mClipArea->getClipRect(); }
169 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
170 bool clipIsSimple() const { return mClipArea->isSimple(); }
171 const ClipArea& getClipArea() const { return *mClipArea; }
174 * Resets the clip to the specified rect.
176 void resetClip(float left, float top, float right, float bottom);
179 * Resets the current transform to a pure 3D translation.
181 void resetTransform(float x, float y, float z);
183 void initializeViewport(int width, int height) {
184 mViewportData.initialize(width, height);
185 mClipAreaRoot.setViewportDimensions(width, height);
188 int getViewportWidth() const { return mViewportData.mWidth; }
189 int getViewportHeight() const { return mViewportData.mHeight; }
190 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
192 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
193 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
196 * Sets (and replaces) the current clipping outline
198 * If the current round rect clip is high priority, the incoming clip is ignored.
200 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
201 float radius, bool highPriority);
204 * Sets (and replaces) the current projection mask
206 void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
209 * Indicates whether this snapshot should be ignored. A snapshot
210 * is typically ignored if its layer is invisible or empty.
212 bool isIgnored() const;
215 * Indicates whether the current transform has perspective components.
217 bool hasPerspectiveTransform() const;
220 * Fills outTransform with the current, total transform to screen space,
221 * across layer boundaries.
223 void buildScreenSpaceTransform(Matrix4* outTransform) const;
233 sp<Snapshot> previous;
236 * A pointer to the currently active layer.
238 * This snapshot does not own the layer, this pointer must not be freed.
243 * Target FBO used for rendering. Set to 0 when rendering directly
244 * into the framebuffer.
249 * Indicates that this snapshot is invisible and nothing should be drawn
250 * inside it. This flag is set only when the layer clips drawing to its
251 * bounds and is passed to subsequent snapshots.
256 * If set to true, the layer will not be composited. This is similar to
257 * invisible but this flag is not passed to subsequent snapshots.
262 * Local transformation. Holds the current translation, scale and
265 * This is a reference to a matrix owned by this snapshot or another
266 * snapshot. This pointer must not be freed. See ::mTransformRoot.
271 * The ancestor layer's dirty region.
273 * This is a reference to a region owned by a layer. This pointer must
279 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
280 * has translucent rendering in a non-overlapping View. This value will be used by
281 * the renderer to set the alpha in the current color being used for ensuing drawing
282 * operations. The value is inherited by child snapshots because the same value should
283 * be applied to descendants of the current DisplayList (for example, a TextView contains
284 * the base alpha value which should be applied to the child DisplayLists used for drawing
290 * Current clipping round rect.
292 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
295 const RoundRectClipState* roundRectClipState;
298 * Current projection masking path - used exclusively to mask tessellated circles.
300 const ProjectionPathMask* projectionPathMask;
305 struct ViewportData {
306 ViewportData() : mWidth(0), mHeight(0) {}
307 void initialize(int width, int height) {
310 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
314 * Width and height of current viewport.
316 * The viewport is always defined to be (0, 0, width, height).
321 * Contains the current orthographic, projection matrix.
328 ClipArea mClipAreaRoot;
332 ViewportData mViewportData;
333 Vector3 mRelativeLightCenter;
337 }; // namespace uirenderer
338 }; // namespace android
340 #endif // ANDROID_HWUI_SNAPSHOT_H