OSDN Git Service

Delete TextureGenerator operations outside the lock
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / LayerAndroid.h
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 "RefPtr.h"
29 #include "SkBitmap.h"
30 #include "SkColor.h"
31 #include "SkRegion.h"
32 #include "SkStream.h"
33 #include "TransformationMatrix.h"
34
35 #include <wtf/HashMap.h>
36
37 #ifndef BZERO_DEFINED
38 #define BZERO_DEFINED
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)
42 #endif
43
44 class SkBitmapRef;
45 class SkCanvas;
46 class SkMatrix;
47 class SkPicture;
48
49 namespace WebCore {
50 class LayerAndroid;
51 class ImageTexture;
52 }
53
54 namespace android {
55 class DrawExtra;
56 void serializeLayer(WebCore::LayerAndroid* layer, SkWStream* stream);
57 WebCore::LayerAndroid* deserializeLayer(SkStream* stream);
58 void cleanupImageRefs(WebCore::LayerAndroid* layer);
59 }
60
61 using namespace android;
62
63 struct SkLength {
64     enum SkLengthType { Undefined, Auto, Relative, Percent, Fixed, Static, Intrinsic, MinIntrinsic };
65     SkLengthType type;
66     SkScalar value;
67     SkLength()
68     {
69         type = Undefined;
70         value = 0;
71     }
72     bool defined() const
73     {
74         if (type == Undefined)
75             return false;
76         return true;
77     }
78     float calcFloatValue(float max) const
79     {
80         switch (type) {
81         case Percent:
82             return (max * value) / 100.0f;
83         case Fixed:
84             return value;
85         default:
86             return value;
87         }
88     }
89 };
90
91 namespace WebCore {
92
93 class AndroidAnimation;
94 class BaseTileTexture;
95 class GLWebViewState;
96 class LayerAndroidFindState;
97 class RenderLayer;
98 class TiledPage;
99 class PaintedSurface;
100
101 class TexturesResult {
102 public:
103     TexturesResult()
104         : fixed(0)
105         , scrollable(0)
106         , clipped(0)
107         , full(0)
108     {}
109
110     int fixed;
111     int scrollable;
112     int clipped;
113     int full;
114 };
115
116 class TEST_EXPORT LayerAndroid : public Layer {
117 public:
118     enum LayerType { UndefinedLayer, WebCoreLayer, UILayer, NavCacheLayer };
119
120     LayerAndroid(RenderLayer* owner);
121     LayerAndroid(const LayerAndroid& layer);
122     LayerAndroid(SkPicture*);
123     virtual ~LayerAndroid();
124
125     virtual TiledPage* page() { return 0; }
126
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
133     // transformation.
134     SkRect bounds() const;
135     IntRect clippedRect() const;
136     bool outsideViewport();
137
138     IntRect unclippedArea();
139     IntRect visibleArea();
140
141     virtual bool needsTexture();
142     void removeTexture(PaintedSurface*);
143
144     // Debug helper methods
145     int nbLayers();
146     int nbTexturedLayers();
147     void showLayer(int indent = 0);
148
149     void computeTexturesAmount(TexturesResult*);
150
151     float getScale() { return m_scale; }
152
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*);
158
159     // prepare layer and its children via reverse-Z, post-order traversal
160     void prepare();
161
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; }
167
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; }
177
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
188         m_fixedLeft = left;
189         m_fixedTop = top;
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;
197         m_isFixed = true;
198         m_renderLayerPos = renderLayerPos;
199         setShouldInheritFromRootTransform(true);
200     }
201
202     void setBackgroundColor(SkColor color);
203     void setMaskLayer(LayerAndroid*);
204     void setMasksToBounds(bool masksToBounds)
205     {
206         m_haveClip = masksToBounds;
207     }
208     bool masksToBounds() const { return m_haveClip; }
209
210     SkPicture* recordContext();
211
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;
218     void addDirtyArea();
219
220     SkPicture* picture() const { return m_recordingPicture; }
221
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,
224     // in global space.
225     SkRect subtractLayers(const SkRect&) const;
226
227     void dumpLayers(FILE*, int indentLevel) const;
228     void dumpToLog() const;
229
230     /** Call this with the current viewport (scrolling, zoom) to update
231         the position of the fixed layers.
232
233         This call is recursive, so it should be called on the root of the
234         hierarchy.
235     */
236     bool updateFixedLayersPositions(SkRect viewPort, LayerAndroid* parentIframeLayer = 0);
237
238     /** Call this to update the position attribute, so that later calls
239         like bounds() will report the corrected position.
240
241         This call is recursive, so it should be called on the root of the
242         hierarchy.
243      */
244     void updatePositions();
245
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
249     {
250         return const_cast<LayerAndroid*>(this)->findById(uniqueID);
251     }
252     LayerAndroid* findById(int uniqueID);
253     LayerAndroid* getChild(int index) const
254     {
255         return static_cast<LayerAndroid*>(this->INHERITED::getChild(index));
256     }
257     int uniqueId() const { return m_uniqueId; }
258     bool isFixed() { return m_isFixed; }
259
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
264         between layers.
265     */
266     void setContentsImage(SkBitmapRef* img);
267
268     void bounds(SkRect*) const;
269
270     virtual LayerAndroid* copy() const { return new LayerAndroid(*this); }
271
272     void needsRepaint() { m_pictureUsed++; }
273     unsigned int pictureUsed() { return m_pictureUsed; }
274
275     void clearDirtyRegion();
276
277     void contentDraw(SkCanvas*);
278
279     virtual bool isMedia() const { return false; }
280     virtual bool isVideo() const { return false; }
281
282     RenderLayer* owningLayer() const { return m_owningLayer; }
283
284     void setIsIframe(bool isIframe) { m_isIframe = isIframe; }
285     float zValue() const { return m_zValue; }
286
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);
291
292     PaintedSurface* texture() { return m_texture; }
293     void obtainTextureForPainting(LayerAndroid* drawingLayer);
294
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*);
300
301     int type() { return m_type; }
302
303     bool hasText() { return m_hasText; }
304     void checkTextPresence();
305
306     void copyAnimationStartTimesRecursive(LayerAndroid* oldTree);
307
308 // rendering asset management
309     void swapTiles();
310     void setIsDrawing(bool isDrawing);
311     void setIsPainting(Layer* drawingTree);
312     void mergeInvalsInto(Layer* replacementTree);
313     bool isReady();
314
315 protected:
316     virtual void onDraw(SkCanvas*, SkScalar opacity);
317
318     TransformationMatrix m_drawTransform;
319
320 private:
321     class FindState;
322 #if DUMP_NAV_CACHE
323     friend class CachedLayer::Debug; // debugging access only
324 #endif
325
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;
330
331     // -------------------------------------------------------------------
332     // Fields to be serialized
333     // -------------------------------------------------------------------
334
335     bool m_haveClip;
336     bool m_isFixed;
337     bool m_backgroundColorSet;
338     bool m_isIframe;
339
340     SkLength m_fixedLeft;
341     SkLength m_fixedTop;
342     SkLength m_fixedRight;
343     SkLength m_fixedBottom;
344     SkLength m_fixedMarginLeft;
345     SkLength m_fixedMarginTop;
346     SkLength m_fixedMarginRight;
347     SkLength m_fixedMarginBottom;
348     SkRect m_fixedRect;
349
350     // When fixed element is undefined or auto, the render layer's position
351     // is needed for offset computation
352     IntPoint m_renderLayerPos;
353
354     bool m_backfaceVisibility;
355     bool m_visible;
356
357     SkColor m_backgroundColor;
358
359     bool m_preserves3D;
360     float m_anchorPointZ;
361     float m_drawOpacity;
362
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;
370
371     typedef HashMap<pair<String, int>, RefPtr<AndroidAnimation> > KeyframesMap;
372     KeyframesMap m_animations;
373
374     TransformationMatrix m_transform;
375     TransformationMatrix m_childrenTransform;
376
377     // -------------------------------------------------------------------
378     // Fields that are not serialized (generated, cached, or non-serializable)
379     // -------------------------------------------------------------------
380
381     SkPoint m_iframeOffset;
382
383     float m_zValue;
384
385     FloatRect m_clippingRect;
386
387     int m_uniqueId;
388
389     PaintedSurface* m_texture;
390     unsigned m_imageCRC;
391
392     unsigned int m_pictureUsed;
393
394     // used to signal the framework we need a repaint
395     bool m_hasRunningAnimations;
396
397     float m_scale;
398
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;
402
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;
407
408     RenderLayer* m_owningLayer;
409
410     int m_type;
411
412     bool m_hasText;
413
414     typedef Layer INHERITED;
415 };
416
417 }
418
419 #else
420
421 class SkPicture;
422
423 namespace WebCore {
424
425 class LayerAndroid {
426 public:
427     LayerAndroid(SkPicture* picture) :
428         m_recordingPicture(picture), // does not assign ownership
429         m_uniqueId(-1)
430     {}
431     SkPicture* picture() const { return m_recordingPicture; }
432     int uniqueId() const { return m_uniqueId; }
433 private:
434     SkPicture* m_recordingPicture;
435     int m_uniqueId;
436 };
437
438 }
439
440 #endif // USE(ACCELERATED_COMPOSITING)
441
442 #endif // LayerAndroid_h