1 /* 2 * Copyright (C) 2009 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 LayerAndroid_h 18 #define LayerAndroid_h 19 20 #if USE(ACCELERATED_COMPOSITING) 21 22 #include "FloatPoint.h" 23 #include "FloatPoint3D.h" 24 #include "FloatRect.h" 25 #include "GraphicsLayerClient.h" 26 #include "ImageTexture.h" 27 #include "Layer.h" 28 #include "PlatformString.h" 29 #include "RefPtr.h" 30 #include "SkBitmap.h" 31 #include "SkColor.h" 32 #include "SkRegion.h" 33 #include "SkStream.h" 34 #include "TransformationMatrix.h" 35 36 #include <utils/threads.h> 37 #include <wtf/HashMap.h> 38 39 #ifndef BZERO_DEFINED 40 #define BZERO_DEFINED 41 // http://www.opengroup.org/onlinepubs/000095399/functions/bzero.html 42 // For maximum portability, it is recommended to replace the function call to bzero() as follows: 43 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0) 44 #endif 45 46 class SkBitmapRef; 47 class SkCanvas; 48 class SkMatrix; 49 class SkPicture; 50 51 namespace WebCore { 52 class LayerAndroid; 53 class LayerContent; 54 class ImageTexture; 55 class Surface; 56 } 57 58 namespace android { 59 class DrawExtra; 60 void serializeLayer(WebCore::LayerAndroid* layer, SkWStream* stream); 61 WebCore::LayerAndroid* deserializeLayer(int version, SkStream* stream); 62 void cleanupImageRefs(WebCore::LayerAndroid* layer); 63 } 64 65 using namespace android; 66 67 namespace WebCore { 68 69 class AndroidAnimation; 70 class FixedPositioning; 71 class GLWebViewState; 72 class IFrameLayerAndroid; 73 class LayerMergeState; 74 class RenderLayer; 75 class PaintedSurface; 76 77 class TexturesResult { 78 public: TexturesResult()79 TexturesResult() 80 : fixed(0) 81 , scrollable(0) 82 , clipped(0) 83 , full(0) 84 {} 85 86 int fixed; 87 int scrollable; 88 int clipped; 89 int full; 90 }; 91 92 class TEST_EXPORT LayerAndroid : public Layer { 93 public: 94 typedef enum { UndefinedLayer, WebCoreLayer, UILayer } LayerType; 95 typedef enum { StandardLayer, ScrollableLayer, 96 IFrameLayer, IFrameContentLayer, 97 FixedBackgroundLayer, 98 FixedBackgroundImageLayer, 99 ForegroundBaseLayer, 100 CanvasLayer, BaseLayer } SubclassType; 101 typedef enum { InvalidateNone = 0, InvalidateLayers } InvalidateFlags; 102 subclassName()103 String subclassName() 104 { 105 switch (subclassType()) { 106 case LayerAndroid::StandardLayer: 107 return "StandardLayer"; 108 case LayerAndroid::ScrollableLayer: 109 return "ScrollableLayer"; 110 case LayerAndroid::IFrameLayer: 111 return "IFrameLayer"; 112 case LayerAndroid::IFrameContentLayer: 113 return "IFrameContentLayer"; 114 case LayerAndroid::FixedBackgroundLayer: 115 return "FixedBackgroundLayer"; 116 case LayerAndroid::FixedBackgroundImageLayer: 117 return "FixedBackgroundImageLayer"; 118 case LayerAndroid::ForegroundBaseLayer: 119 return "ForegroundBaseLayer"; 120 case LayerAndroid::CanvasLayer: 121 return "CanvasLayer"; 122 case LayerAndroid::BaseLayer: 123 return "BaseLayer"; 124 } 125 return "Undefined"; 126 } 127 128 LayerAndroid(RenderLayer* owner); 129 LayerAndroid(const LayerAndroid& layer); 130 virtual ~LayerAndroid(); 131 setBackfaceVisibility(bool value)132 void setBackfaceVisibility(bool value) { m_backfaceVisibility = value; } setTransform(const TransformationMatrix & matrix)133 void setTransform(const TransformationMatrix& matrix) { m_transform = matrix; } 134 FloatPoint translation() const; 135 IntRect clippedRect() const; 136 bool outsideViewport(); 137 138 // Returns the full area of the layer mapped into global content coordinates 139 FloatRect fullContentAreaMapped() const; 140 141 IntRect fullContentArea() const; 142 IntRect visibleContentArea(bool force3dContentVisible = false) const; 143 144 virtual bool needsTexture(); 145 146 // Debug helper methods 147 int nbLayers(); 148 int nbTexturedLayers(); 149 void showLayer(int indent = 0); 150 getScale()151 float getScale() { return m_scale; } 152 153 virtual bool drawGL(bool layerTilesDisabled); 154 virtual bool drawCanvas(SkCanvas* canvas, bool drawChildren, PaintStyle style); 155 bool drawChildrenCanvas(SkCanvas* canvas, PaintStyle style); 156 157 void updateGLPositionsAndScale(const TransformationMatrix& parentMatrix, 158 const FloatRect& clip, float opacity, float scale, 159 bool forceCalculations, bool disableFixedElemUpdate); setDrawOpacity(float opacity)160 void setDrawOpacity(float opacity) { m_drawOpacity = opacity; } drawOpacity()161 float drawOpacity() { return m_drawOpacity; } 162 bool visible(); setVisible(bool value)163 void setVisible(bool value) { m_visible = value; } 164 preserves3D()165 bool preserves3D() { return m_preserves3D; } setPreserves3D(bool value)166 void setPreserves3D(bool value) { m_preserves3D = value; } setAnchorPointZ(float z)167 void setAnchorPointZ(float z) { m_anchorPointZ = z; } anchorPointZ()168 float anchorPointZ() { return m_anchorPointZ; } setDrawTransform(const TransformationMatrix & transform)169 void setDrawTransform(const TransformationMatrix& transform) { m_drawTransform = m_drawTransformUnfudged = transform; } drawTransform()170 virtual const TransformationMatrix* drawTransform() const { return &m_drawTransform; } setChildrenTransform(const TransformationMatrix & t)171 void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; } setDrawClip(const FloatRect & rect)172 void setDrawClip(const FloatRect& rect) { m_clippingRect = rect; } drawClip()173 const FloatRect& drawClip() { return m_clippingRect; } 174 175 void setBackgroundColor(SkColor color); 176 void setMaskLayer(LayerAndroid*); setMasksToBounds(bool masksToBounds)177 void setMasksToBounds(bool masksToBounds) 178 { 179 m_haveClip = masksToBounds; 180 } masksToBounds()181 bool masksToBounds() const { return m_haveClip; } 182 content()183 LayerContent* content() { return m_content; } 184 void setContent(LayerContent* content); 185 // Check to see if the dirty area of this layer can be updated with a blit 186 // from the prerender instead of needing to generate tiles from the LayerContent 187 bool canUpdateWithBlit(); 188 189 void addAnimation(PassRefPtr<AndroidAnimation> anim); 190 void removeAnimationsForProperty(AnimatedPropertyID property); 191 void removeAnimationsForKeyframes(const String& name); 192 bool evaluateAnimations(); 193 bool evaluateAnimations(double time); 194 void initAnimations(); 195 bool hasAnimations() const; 196 void addDirtyArea(); 197 198 virtual void dumpLayer(FILE*, int indentLevel) const; 199 void dumpLayers(FILE*, int indentLevel) const; 200 void dumpToLog() const; 201 202 virtual IFrameLayerAndroid* updatePosition(SkRect viewport, 203 IFrameLayerAndroid* parentIframeLayer); 204 205 /** Call this to update the position attribute, so that later calls 206 like bounds() will report the corrected position. 207 208 This call is recursive, so it should be called on the root of the 209 hierarchy. 210 */ 211 void updatePositions(); 212 213 const LayerAndroid* find(int* xPtr, int* yPtr, SkPicture* root) const; findById(int uniqueID)214 const LayerAndroid* findById(int uniqueID) const 215 { 216 return const_cast<LayerAndroid*>(this)->findById(uniqueID); 217 } 218 LayerAndroid* findById(int uniqueID); getChild(int index)219 LayerAndroid* getChild(int index) const 220 { 221 return static_cast<LayerAndroid*>(this->INHERITED::getChild(index)); 222 } uniqueId()223 int uniqueId() const { return m_uniqueId; } 224 225 /** This sets a content image -- calling it means we will use 226 the image directly when drawing the layer instead of using 227 the content painted by WebKit. 228 Images are handled in ImagesManager, as they can be shared 229 between layers. 230 */ 231 void setContentsImage(SkBitmapRef* img); 232 copy()233 virtual LayerAndroid* copy() const { return new LayerAndroid(*this); } 234 235 virtual void clearDirtyRegion(); 236 237 virtual void contentDraw(SkCanvas* canvas, PaintStyle style); 238 isMedia()239 virtual bool isMedia() const { return false; } isVideo()240 virtual bool isVideo() const { return false; } isIFrame()241 virtual bool isIFrame() const { return false; } isIFrameContent()242 virtual bool isIFrameContent() const { return false; } isFixedBackground()243 virtual bool isFixedBackground() const { return false; } 244 isPositionFixed()245 bool isPositionFixed() const { return m_fixedPosition; } setAbsolutePosition(bool isAbsolute)246 void setAbsolutePosition(bool isAbsolute) { m_isPositionAbsolute = isAbsolute; } isPositionAbsolute()247 bool isPositionAbsolute() { return m_isPositionAbsolute; } 248 void setFixedPosition(FixedPositioning* position); fixedPosition()249 FixedPositioning* fixedPosition() { return m_fixedPosition; } 250 owningLayer()251 RenderLayer* owningLayer() const { return m_owningLayer; } 252 zValue()253 float zValue() const { return m_zValue; } 254 255 // ViewStateSerializer friends 256 friend void android::serializeLayer(LayerAndroid* layer, SkWStream* stream); 257 friend LayerAndroid* android::deserializeLayer(int version, SkStream* stream); 258 friend void android::cleanupImageRefs(LayerAndroid* layer); 259 type()260 LayerType type() { return m_type; } subclassType()261 virtual SubclassType subclassType() const { return LayerAndroid::StandardLayer; } 262 263 bool hasText(); 264 265 void copyAnimationStartTimesRecursive(LayerAndroid* oldTree); 266 267 // rendering asset management getInvalRegion()268 SkRegion* getInvalRegion() { return &m_dirtyRegion; } 269 void mergeInvalsInto(LayerAndroid* replacementTree); 270 271 bool canJoinSurface(Surface* surface); 272 void assignSurfaces(LayerMergeState* mergeState); surface()273 Surface* surface() { return m_surface; } 274 setIntrinsicallyComposited(bool intCom)275 void setIntrinsicallyComposited(bool intCom) { m_intrinsicallyComposited = intCom; } needsIsolatedSurface()276 virtual bool needsIsolatedSurface() { 277 return (needsTexture() && m_intrinsicallyComposited) 278 || m_animations.size() 279 || m_imageCRC; 280 } 281 282 int setHwAccelerated(bool hwAccelerated); 283 setReplicatedLayer(LayerAndroid * layer)284 void setReplicatedLayer(LayerAndroid* layer) { m_replicatedLayer = layer; } setReplicatedLayerPosition(const FloatPoint & p)285 void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; } setOriginalLayer(LayerAndroid * layer)286 void setOriginalLayer(LayerAndroid* layer) { m_originalLayer = layer; } hasReplicatedLayer()287 bool hasReplicatedLayer() { return m_replicatedLayer; } replicatedLayerDrawTransform()288 const TransformationMatrix* replicatedLayerDrawTransform() { 289 if (m_replicatedLayer) 290 return m_replicatedLayer->drawTransform(); 291 return 0; 292 } 293 294 protected: 295 /** Call this with the current viewport (scrolling, zoom) to update 296 the position of the fixed layers. 297 298 This call is recursive, so it should be called on the root of the 299 hierarchy. 300 */ 301 void updateLayerPositions(SkRect viewPort, IFrameLayerAndroid* parentIframeLayer = 0); 302 virtual void onDraw(SkCanvas*, SkScalar opacity, android::DrawExtra* extra, PaintStyle style); onSetHwAccelerated(bool hwAccelerated)303 virtual InvalidateFlags onSetHwAccelerated(bool hwAccelerated) { return InvalidateNone; } 304 TransformationMatrix m_drawTransform; 305 TransformationMatrix m_drawTransformUnfudged; 306 int m_uniqueId; 307 308 private: 309 void updateLocalTransformAndClip(const TransformationMatrix& parentMatrix, 310 const FloatRect& clip); hasDynamicTransform()311 bool hasDynamicTransform() { 312 return contentIsScrollable() || isPositionFixed() || (m_animations.size() != 0); 313 } 314 315 #if DUMP_NAV_CACHE 316 friend class CachedLayer::Debug; // debugging access only 317 #endif 318 319 void copyAnimationStartTimes(LayerAndroid* oldLayer); 320 bool prepareContext(bool force = false); 321 322 // ------------------------------------------------------------------- 323 // Fields to be serialized 324 // ------------------------------------------------------------------- 325 326 bool m_haveClip; 327 bool m_backgroundColorSet; 328 329 bool m_backfaceVisibility; 330 bool m_visible; 331 332 protected: 333 SkColor m_backgroundColor; 334 335 private: 336 337 bool m_preserves3D; 338 float m_anchorPointZ; 339 float m_drawOpacity; 340 341 bool m_isPositionAbsolute; 342 343 protected: 344 FixedPositioning* m_fixedPosition; 345 346 private: 347 348 typedef HashMap<pair<String, int>, RefPtr<AndroidAnimation> > KeyframesMap; 349 KeyframesMap m_animations; 350 351 TransformationMatrix m_transform; 352 TransformationMatrix m_childrenTransform; 353 354 // ------------------------------------------------------------------- 355 // Fields that are not serialized (generated, cached, or non-serializable) 356 // ------------------------------------------------------------------- 357 358 float m_zValue; 359 360 FloatRect m_clippingRect; 361 362 // Note that m_content and m_imageCRC are mutually exclusive; 363 // m_content is used when WebKit is asked to paint the layer's 364 // content, while m_imageCRC references an image that we directly 365 // composite, using the layer's dimensions as a destination rect. 366 // We do this as if the layer only contains an image, directly compositing 367 // it is a much faster method than using m_content. 368 LayerContent* m_content; 369 370 protected: 371 unsigned m_imageCRC; 372 373 private: 374 375 // used to signal the framework we need a repaint 376 bool m_hasRunningAnimations; 377 378 float m_scale; 379 380 // We try to not always compute the texture size, as this is quite heavy 381 static const double s_computeTextureDelay = 0.2; // 200 ms 382 double m_lastComputeTextureSize; 383 384 RenderLayer* m_owningLayer; 385 386 LayerType m_type; 387 SubclassType m_subclassType; 388 389 bool m_intrinsicallyComposited; 390 391 Surface* m_surface; 392 393 // link to a replicated layer (used e.g. for reflections) 394 LayerAndroid* m_replicatedLayer; 395 FloatPoint m_replicatedLayerPosition; 396 LayerAndroid* m_originalLayer; 397 // link to a mask layer 398 LayerAndroid* m_maskLayer; 399 400 typedef Layer INHERITED; 401 }; 402 403 } 404 405 #else 406 407 class SkPicture; 408 409 namespace WebCore { 410 411 class LayerAndroid { 412 public: LayerAndroid(SkPicture * picture)413 LayerAndroid(SkPicture* picture) : 414 m_recordingPicture(picture), // does not assign ownership 415 m_uniqueId(-1) 416 {} picture()417 SkPicture* picture() const { return m_recordingPicture; } uniqueId()418 int uniqueId() const { return m_uniqueId; } 419 private: 420 SkPicture* m_recordingPicture; 421 int m_uniqueId; 422 }; 423 424 } 425 426 #endif // USE(ACCELERATED_COMPOSITING) 427 428 #endif // LayerAndroid_h 429