2 * Copyright (C) 2009 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 LayerAndroid_h
18 #define LayerAndroid_h
20 #if USE(ACCELERATED_COMPOSITING)
22 #include "FloatPoint.h"
23 #include "FloatPoint3D.h"
24 #include "FloatRect.h"
25 #include "GraphicsLayerClient.h"
26 #include "ImageTexture.h"
33 #include "TransformationMatrix.h"
35 #include <wtf/HashMap.h>
39 // http://www.opengroup.org/onlinepubs/000095399/functions/bzero.html
40 // For maximum portability, it is recommended to replace the function call to bzero() as follows:
41 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
56 void serializeLayer(WebCore::LayerAndroid* layer, SkWStream* stream);
57 WebCore::LayerAndroid* deserializeLayer(SkStream* stream);
58 void cleanupImageRefs(WebCore::LayerAndroid* layer);
61 using namespace android;
64 enum SkLengthType { Undefined, Auto, Relative, Percent, Fixed, Static, Intrinsic, MinIntrinsic };
74 if (type == Undefined)
78 float calcFloatValue(float max) const
82 return (max * value) / 100.0f;
93 class AndroidAnimation;
94 class BaseTileTexture;
96 class LayerAndroidFindState;
101 class TexturesResult {
116 class TEST_EXPORT LayerAndroid : public Layer {
118 enum LayerType { UndefinedLayer, WebCoreLayer, UILayer, NavCacheLayer };
120 LayerAndroid(RenderLayer* owner);
121 LayerAndroid(const LayerAndroid& layer);
122 LayerAndroid(SkPicture*);
123 virtual ~LayerAndroid();
125 virtual TiledPage* page() { return 0; }
127 void setBackfaceVisibility(bool value) { m_backfaceVisibility = value; }
128 void setTransform(const TransformationMatrix& matrix) { m_transform = matrix; }
129 FloatPoint translation() const;
130 // Returns a rect describing the bounds of the layer with the local
131 // transformation applied, expressed relative to the parent layer.
132 // FIXME: Currently we use only the translation component of the local
134 SkRect bounds() const;
135 IntRect clippedRect() const;
136 bool outsideViewport();
138 IntRect unclippedArea();
139 IntRect visibleArea();
141 virtual bool needsTexture();
142 void removeTexture(PaintedSurface*);
144 // Debug helper methods
146 int nbTexturedLayers();
147 void showLayer(int indent = 0);
149 void computeTexturesAmount(TexturesResult*);
151 float getScale() { return m_scale; }
153 // draw layer and its children via Z, pre-order traversal
154 virtual bool drawGL();
155 bool drawChildrenGL();
156 virtual bool drawCanvas(SkCanvas*);
157 bool drawChildrenCanvas(SkCanvas*);
159 // prepare layer and its children via reverse-Z, post-order traversal
162 void updateGLPositionsAndScale(const TransformationMatrix& parentMatrix,
163 const FloatRect& clip, float opacity, float scale);
164 void setDrawOpacity(float opacity) { m_drawOpacity = opacity; }
165 float drawOpacity() { return m_drawOpacity; }
166 void setVisible(bool value) { m_visible = value; }
168 bool preserves3D() { return m_preserves3D; }
169 void setPreserves3D(bool value) { m_preserves3D = value; }
170 void setAnchorPointZ(float z) { m_anchorPointZ = z; }
171 float anchorPointZ() { return m_anchorPointZ; }
172 void setDrawTransform(const TransformationMatrix& transform) { m_drawTransform = transform; }
173 const TransformationMatrix* drawTransform() const { return &m_drawTransform; }
174 void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
175 void setDrawClip(const FloatRect& rect) { m_clippingRect = rect; }
176 const FloatRect& drawClip() { return m_clippingRect; }
178 void setFixedPosition(SkLength left, // CSS left property
179 SkLength top, // CSS top property
180 SkLength right, // CSS right property
181 SkLength bottom, // CSS bottom property
182 SkLength marginLeft, // CSS margin-left property
183 SkLength marginTop, // CSS margin-top property
184 SkLength marginRight, // CSS margin-right property
185 SkLength marginBottom, // CSS margin-bottom property
186 const IntPoint& renderLayerPos, // For undefined fixed position
187 SkRect viewRect) { // view rect, can be smaller than the layer's
190 m_fixedRight = right;
191 m_fixedBottom = bottom;
192 m_fixedMarginLeft = marginLeft;
193 m_fixedMarginTop = marginTop;
194 m_fixedMarginRight = marginRight;
195 m_fixedMarginBottom = marginBottom;
196 m_fixedRect = viewRect;
198 m_renderLayerPos = renderLayerPos;
199 setShouldInheritFromRootTransform(true);
202 void setBackgroundColor(SkColor color);
203 void setMaskLayer(LayerAndroid*);
204 void setMasksToBounds(bool masksToBounds)
206 m_haveClip = masksToBounds;
208 bool masksToBounds() const { return m_haveClip; }
210 SkPicture* recordContext();
212 void addAnimation(PassRefPtr<AndroidAnimation> anim);
213 void removeAnimationsForProperty(AnimatedPropertyID property);
214 void removeAnimationsForKeyframes(const String& name);
215 bool evaluateAnimations();
216 bool evaluateAnimations(double time);
217 bool hasAnimations() const;
220 SkPicture* picture() const { return m_recordingPicture; }
222 // Given a rect in global space, subtracts from it the bounds of this layer
223 // and of all of its children. Returns the bounding rectangle of the result,
225 SkRect subtractLayers(const SkRect&) const;
227 void dumpLayers(FILE*, int indentLevel) const;
228 void dumpToLog() const;
230 /** Call this with the current viewport (scrolling, zoom) to update
231 the position of the fixed layers.
233 This call is recursive, so it should be called on the root of the
236 bool updateFixedLayersPositions(SkRect viewPort, LayerAndroid* parentIframeLayer = 0);
238 /** Call this to update the position attribute, so that later calls
239 like bounds() will report the corrected position.
241 This call is recursive, so it should be called on the root of the
244 void updatePositions();
246 void clipArea(SkTDArray<SkRect>* region) const;
247 const LayerAndroid* find(int* xPtr, int* yPtr, SkPicture* root) const;
248 const LayerAndroid* findById(int uniqueID) const
250 return const_cast<LayerAndroid*>(this)->findById(uniqueID);
252 LayerAndroid* findById(int uniqueID);
253 LayerAndroid* getChild(int index) const
255 return static_cast<LayerAndroid*>(this->INHERITED::getChild(index));
257 int uniqueId() const { return m_uniqueId; }
258 bool isFixed() { return m_isFixed; }
260 /** This sets a content image -- calling it means we will use
261 the image directly when drawing the layer instead of using
262 the content painted by WebKit.
263 Images are handled in ImagesManager, as they can be shared
266 void setContentsImage(SkBitmapRef* img);
268 void bounds(SkRect*) const;
270 virtual LayerAndroid* copy() const { return new LayerAndroid(*this); }
272 void needsRepaint() { m_pictureUsed++; }
273 unsigned int pictureUsed() { return m_pictureUsed; }
275 void clearDirtyRegion();
277 void contentDraw(SkCanvas*);
279 virtual bool isMedia() const { return false; }
280 virtual bool isVideo() const { return false; }
282 RenderLayer* owningLayer() const { return m_owningLayer; }
284 void setIsIframe(bool isIframe) { m_isIframe = isIframe; }
285 float zValue() const { return m_zValue; }
287 // ViewStateSerializer friends
288 friend void android::serializeLayer(LayerAndroid* layer, SkWStream* stream);
289 friend LayerAndroid* android::deserializeLayer(SkStream* stream);
290 friend void android::cleanupImageRefs(LayerAndroid* layer);
292 PaintedSurface* texture() { return m_texture; }
293 void obtainTextureForPainting(LayerAndroid* drawingLayer);
295 // Update layers using another tree. Only works for basic properties
296 // such as the position, the transform. Return true if anything more
297 // complex is needed.
298 bool updateWithTree(LayerAndroid*);
299 virtual bool updateWithLayer(LayerAndroid*);
301 int type() { return m_type; }
303 bool hasText() { return m_hasText; }
304 void checkTextPresence();
306 void copyAnimationStartTimesRecursive(LayerAndroid* oldTree);
308 // rendering asset management
310 void setIsDrawing(bool isDrawing);
311 void setIsPainting(Layer* drawingTree);
312 void mergeInvalsInto(Layer* replacementTree);
316 virtual void onDraw(SkCanvas*, SkScalar opacity);
318 TransformationMatrix m_drawTransform;
323 friend class CachedLayer::Debug; // debugging access only
326 void copyAnimationStartTimes(LayerAndroid* oldLayer);
327 void findInner(FindState&) const;
328 bool prepareContext(bool force = false);
329 void clipInner(SkTDArray<SkRect>* region, const SkRect& local) const;
331 // -------------------------------------------------------------------
332 // Fields to be serialized
333 // -------------------------------------------------------------------
337 bool m_backgroundColorSet;
340 SkLength m_fixedLeft;
342 SkLength m_fixedRight;
343 SkLength m_fixedBottom;
344 SkLength m_fixedMarginLeft;
345 SkLength m_fixedMarginTop;
346 SkLength m_fixedMarginRight;
347 SkLength m_fixedMarginBottom;
350 // When fixed element is undefined or auto, the render layer's position
351 // is needed for offset computation
352 IntPoint m_renderLayerPos;
354 bool m_backfaceVisibility;
357 SkColor m_backgroundColor;
360 float m_anchorPointZ;
363 // Note that m_recordingPicture and m_imageRef are mutually exclusive;
364 // m_recordingPicture is used when WebKit is asked to paint the layer's
365 // content, while m_imageRef contains an image that we directly
366 // composite, using the layer's dimensions as a destination rect.
367 // We do this as if the layer only contains an image, directly compositing
368 // it is a much faster method than using m_recordingPicture.
369 SkPicture* m_recordingPicture;
371 typedef HashMap<pair<String, int>, RefPtr<AndroidAnimation> > KeyframesMap;
372 KeyframesMap m_animations;
374 TransformationMatrix m_transform;
375 TransformationMatrix m_childrenTransform;
377 // -------------------------------------------------------------------
378 // Fields that are not serialized (generated, cached, or non-serializable)
379 // -------------------------------------------------------------------
381 SkPoint m_iframeOffset;
385 FloatRect m_clippingRect;
389 PaintedSurface* m_texture;
392 unsigned int m_pictureUsed;
394 // used to signal the framework we need a repaint
395 bool m_hasRunningAnimations;
399 // We try to not always compute the texture size, as this is quite heavy
400 static const double s_computeTextureDelay = 0.2; // 200 ms
401 double m_lastComputeTextureSize;
403 // This mutex serves two purposes. (1) It ensures that certain operations
404 // happen atomically and (2) it makes sure those operations are synchronized
405 // across all threads and cores.
406 android::Mutex m_atomicSync;
408 RenderLayer* m_owningLayer;
414 typedef Layer INHERITED;
427 LayerAndroid(SkPicture* picture) :
428 m_recordingPicture(picture), // does not assign ownership
431 SkPicture* picture() const { return m_recordingPicture; }
432 int uniqueId() const { return m_uniqueId; }
434 SkPicture* m_recordingPicture;
440 #endif // USE(ACCELERATED_COMPOSITING)
442 #endif // LayerAndroid_h