Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 
     32 #ifndef LayerRendererChromium_h
     33 #define LayerRendererChromium_h
     34 
     35 #if USE(ACCELERATED_COMPOSITING)
     36 
     37 #include "ContentLayerChromium.h"
     38 #include "IntRect.h"
     39 #include "LayerChromium.h"
     40 #include "LayerTilerChromium.h"
     41 #include "RenderSurfaceChromium.h"
     42 #include "SkBitmap.h"
     43 #include "VideoLayerChromium.h"
     44 #include "cc/CCCanvasLayerImpl.h"
     45 #include "cc/CCHeadsUpDisplay.h"
     46 #include "cc/CCPluginLayerImpl.h"
     47 #include "cc/CCVideoLayerImpl.h"
     48 #include <wtf/HashMap.h>
     49 #include <wtf/Noncopyable.h>
     50 #include <wtf/PassOwnPtr.h>
     51 #include <wtf/PassRefPtr.h>
     52 #include <wtf/RefCounted.h>
     53 #include <wtf/Vector.h>
     54 
     55 #if USE(CG)
     56 #include <CoreGraphics/CGContext.h>
     57 #include <wtf/RetainPtr.h>
     58 #endif
     59 
     60 namespace WebCore {
     61 
     62 class CCHeadsUpDisplay;
     63 class CCLayerImpl;
     64 class GeometryBinding;
     65 class GraphicsContext3D;
     66 
     67 // Class that handles drawing of composited render layers using GL.
     68 class LayerRendererChromium : public RefCounted<LayerRendererChromium> {
     69 public:
     70     static PassRefPtr<LayerRendererChromium> create(PassRefPtr<GraphicsContext3D>, PassOwnPtr<TilePaintInterface> contentPaint);
     71 
     72     ~LayerRendererChromium();
     73 
     74     GraphicsContext3D* context();
     75 
     76     void invalidateRootLayerRect(const IntRect& dirtyRect);
     77 
     78     void setViewport(const IntRect& visibleRect, const IntRect& contentRect, const IntPoint& scrollPosition);
     79 
     80     // updates and draws the current layers onto the backbuffer
     81     void updateAndDrawLayers();
     82 
     83     // waits for rendering to finish
     84     void finish();
     85 
     86     // puts backbuffer onscreen
     87     void present();
     88 
     89     IntSize viewportSize() const { return m_viewportVisibleRect.size(); }
     90 
     91     void setRootLayer(PassRefPtr<LayerChromium>);
     92     LayerChromium* rootLayer() { return m_rootLayer.get(); }
     93     void transferRootLayer(LayerRendererChromium* other) { other->m_rootLayer = m_rootLayer.release(); }
     94 
     95     bool hardwareCompositing() const { return m_hardwareCompositing; }
     96 
     97     void setCompositeOffscreen(bool);
     98     bool isCompositingOffscreen() const { return m_compositeOffscreen; }
     99 
    100     unsigned createLayerTexture();
    101     void deleteLayerTexture(unsigned);
    102 
    103     static void debugGLCall(GraphicsContext3D*, const char* command, const char* file, int line);
    104 
    105     const TransformationMatrix& projectionMatrix() const { return m_projectionMatrix; }
    106 
    107     void useShader(unsigned);
    108 
    109     bool checkTextureSize(const IntSize&);
    110 
    111     const GeometryBinding* sharedGeometry() const { return m_sharedGeometry.get(); }
    112     const LayerChromium::BorderProgram* borderProgram() const { return m_borderProgram.get(); }
    113     const CCHeadsUpDisplay::Program* headsUpDisplayProgram() const { return m_headsUpDisplayProgram.get(); }
    114     const RenderSurfaceChromium::Program* renderSurfaceProgram() const { return m_renderSurfaceProgram.get(); }
    115     const RenderSurfaceChromium::MaskProgram* renderSurfaceMaskProgram() const { return m_renderSurfaceMaskProgram.get(); }
    116     const LayerTilerChromium::Program* tilerProgram() const { return m_tilerProgram.get(); }
    117     const CCCanvasLayerImpl::Program* canvasLayerProgram() const { return m_canvasLayerProgram.get(); }
    118     const CCPluginLayerImpl::Program* pluginLayerProgram() const { return m_pluginLayerProgram.get(); }
    119     const CCVideoLayerImpl::RGBAProgram* videoLayerRGBAProgram() const { return m_videoLayerRGBAProgram.get(); }
    120     const CCVideoLayerImpl::YUVProgram* videoLayerYUVProgram() const { return m_videoLayerYUVProgram.get(); }
    121 
    122     void resizeOnscreenContent(const IntSize&);
    123 
    124     void getFramebufferPixels(void *pixels, const IntRect& rect);
    125 
    126     TextureManager* textureManager() const { return m_textureManager.get(); }
    127 
    128     CCHeadsUpDisplay* headsUpDisplay() { return m_headsUpDisplay.get(); }
    129 
    130     void setScissorToRect(const IntRect&);
    131 
    132     String layerTreeAsText() const;
    133 
    134     void addChildContext(GraphicsContext3D*);
    135     void removeChildContext(GraphicsContext3D*);
    136 
    137 private:
    138     typedef Vector<RefPtr<CCLayerImpl> > LayerList;
    139     typedef HashMap<GraphicsContext3D*, int> ChildContextMap;
    140 
    141     explicit LayerRendererChromium(PassRefPtr<GraphicsContext3D>, PassOwnPtr<TilePaintInterface> contentPaint);
    142 
    143     void updateLayers(LayerList& renderSurfaceLayerList);
    144     void updateRootLayerContents();
    145     void updatePropertiesAndRenderSurfaces(LayerChromium*, const TransformationMatrix& parentMatrix, LayerList& renderSurfaceLayerList, LayerList& layers);
    146 
    147     void paintLayerContents(const LayerList&);
    148     void updateCompositorResourcesRecursive(LayerChromium*);
    149 
    150     void drawLayers(const LayerList& renderSurfaceLayerList);
    151     void drawLayer(CCLayerImpl*, RenderSurfaceChromium*);
    152 
    153     void drawRootLayer();
    154     LayerTexture* getOffscreenLayerTexture();
    155     void copyOffscreenTextureToDisplay();
    156 
    157     bool isLayerVisible(LayerChromium*, const TransformationMatrix&, const IntRect& visibleRect);
    158 
    159     void setDrawViewportRect(const IntRect&, bool flipY);
    160 
    161     bool useRenderSurface(RenderSurfaceChromium*);
    162 
    163     bool makeContextCurrent();
    164 
    165     static bool compareLayerZ(const RefPtr<CCLayerImpl>&, const RefPtr<CCLayerImpl>&);
    166 
    167     void dumpRenderSurfaces(TextStream&, int indent, LayerChromium*) const;
    168 
    169     bool initializeSharedObjects();
    170     void cleanupSharedObjects();
    171 
    172     IntRect m_viewportVisibleRect;
    173     IntRect m_viewportContentRect;
    174     IntPoint m_viewportScrollPosition;
    175 
    176     TransformationMatrix m_projectionMatrix;
    177 
    178     RefPtr<LayerChromium> m_rootLayer;
    179     OwnPtr<TilePaintInterface> m_rootLayerContentPaint;
    180     OwnPtr<LayerTilerChromium> m_rootLayerContentTiler;
    181 
    182     bool m_hardwareCompositing;
    183 
    184     unsigned m_currentShader;
    185     RenderSurfaceChromium* m_currentRenderSurface;
    186 
    187     unsigned m_offscreenFramebufferId;
    188     bool m_compositeOffscreen;
    189 
    190 #if USE(SKIA)
    191     OwnPtr<SkCanvas> m_rootLayerCanvas;
    192     OwnPtr<PlatformContextSkia> m_rootLayerSkiaContext;
    193     OwnPtr<GraphicsContext> m_rootLayerGraphicsContext;
    194 #elif USE(CG)
    195     Vector<uint8_t> m_rootLayerBackingStore;
    196     RetainPtr<CGContextRef> m_rootLayerCGContext;
    197     OwnPtr<GraphicsContext> m_rootLayerGraphicsContext;
    198 #endif
    199 
    200     // Maximum texture dimensions supported.
    201     int m_maxTextureSize;
    202 
    203     // Store values that are shared between instances of each layer type
    204     // associated with this instance of the compositor. Since there can be
    205     // multiple instances of the compositor running in the same renderer process
    206     // we cannot store these values in static variables.
    207     OwnPtr<GeometryBinding> m_sharedGeometry;
    208     OwnPtr<LayerChromium::BorderProgram> m_borderProgram;
    209     OwnPtr<CCHeadsUpDisplay::Program> m_headsUpDisplayProgram;
    210     OwnPtr<RenderSurfaceChromium::Program> m_renderSurfaceProgram;
    211     OwnPtr<RenderSurfaceChromium::MaskProgram> m_renderSurfaceMaskProgram;
    212     OwnPtr<LayerTilerChromium::Program> m_tilerProgram;
    213     OwnPtr<CCCanvasLayerImpl::Program> m_canvasLayerProgram;
    214     OwnPtr<CCVideoLayerImpl::RGBAProgram> m_videoLayerRGBAProgram;
    215     OwnPtr<CCVideoLayerImpl::YUVProgram> m_videoLayerYUVProgram;
    216     OwnPtr<CCPluginLayerImpl::Program> m_pluginLayerProgram;
    217 
    218     OwnPtr<TextureManager> m_textureManager;
    219 
    220     OwnPtr<CCHeadsUpDisplay> m_headsUpDisplay;
    221 
    222     RefPtr<GraphicsContext3D> m_context;
    223     ChildContextMap m_childContexts;
    224 
    225     // If true, the child contexts were copied to the compositor texture targets
    226     // and the compositor will need to wait on the proper latches before using
    227     // the target textures. If false, the compositor is reusing the textures
    228     // from last frame.
    229     bool m_childContextsWereCopied;
    230 
    231     bool m_contextSupportsLatch;
    232 
    233     RenderSurfaceChromium* m_defaultRenderSurface;
    234 };
    235 
    236 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
    237 // call made by the compositor. Useful for debugging rendering issues but
    238 // will significantly degrade performance.
    239 #define DEBUG_GL_CALLS 0
    240 
    241 #if DEBUG_GL_CALLS && !defined ( NDEBUG )
    242 #define GLC(context, x) { (x), LayerRendererChromium::debugGLCall(context, #x, __FILE__, __LINE__); }
    243 #else
    244 #define GLC(context, x) (x)
    245 #endif
    246 
    247 
    248 }
    249 
    250 #endif // USE(ACCELERATED_COMPOSITING)
    251 
    252 #endif
    253