Bug 43363 - [chromium] Add a Canvas2DLayerChromium layer type
Summary: [chromium] Add a Canvas2DLayerChromium layer type
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: 528+ (Nightly build)
Hardware: Other OS X 10.5
: P2 Normal
Assignee: James Robinson
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-02 10:21 PDT by James Robinson
Modified: 2010-09-27 11:35 PDT (History)
4 users (show)

See Also:


Attachments
Patch (11.70 KB, patch)
2010-08-02 10:24 PDT, James Robinson
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description James Robinson 2010-08-02 10:21:51 PDT
[chromium] Add a Canvas2DLayerChromium layer type
Comment 1 James Robinson 2010-08-02 10:24:02 PDT
Created attachment 63231 [details]
Patch
Comment 2 Vangelis Kokkevis 2010-08-02 15:57:59 PDT
Comment on attachment 63231 [details]
Patch


Looks good. I'm not very happy with the amount of code we end up duplicating for each new layer type (we'll have very similar code for the video layer) but that's not necessarily a problem of this checkin.


> diff --git a/WebCore/WebCore.gypi b/WebCore/WebCore.gypi
> index 6983bc8cf77b21747227fe23a949cebed6b81d32..700b6b0459d605cd21e720b92bb9c44d3ba57400 100644
> --- a/WebCore/WebCore.gypi
> +++ b/WebCore/WebCore.gypi
> @@ -2167,6 +2167,8 @@
>              'platform/graphics/cg/PathCG.cpp',
>              'platform/graphics/cg/PatternCG.cpp',
>              'platform/graphics/cg/TransformationMatrixCG.cpp',
> +            'platform/graphics/chromium/Canvas2DLayerChromium.cpp',
> +            'platform/graphics/chromium/Canvas2DLayerChromium.h',
>              'platform/graphics/chromium/FontCacheChromiumWin.cpp',
>              'platform/graphics/chromium/FontCacheLinux.cpp',
>              'platform/graphics/chromium/FontChromiumWin.cpp',
> diff --git a/WebCore/platform/graphics/chromium/Canvas2DLayerChromium.cpp b/WebCore/platform/graphics/chromium/Canvas2DLayerChromium.cpp
> new file mode 100644
> index 0000000000000000000000000000000000000000..5073adb5c2ef00043fd2169bc8c524ba9a3cfc25
> --- /dev/null
> +++ b/WebCore/platform/graphics/chromium/Canvas2DLayerChromium.cpp
> @@ -0,0 +1,88 @@
> +/*
> + * Copyright (C) 2010 Google Inc. All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions are
> + * met:
> + *
> + *     * Redistributions of source code must retain the above copyright
> + * notice, this list of conditions and the following disclaimer.
> + *     * Redistributions in binary form must reproduce the above
> + * copyright notice, this list of conditions and the following disclaimer
> + * in the documentation and/or other materials provided with the
> + * distribution.
> + *     * Neither the name of Google Inc. nor the names of its
> + * contributors may be used to endorse or promote products derived from
> + * this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
> + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
> + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
> + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "config.h"
> +
> +#if ENABLE(ACCELERATED_2D_CANVAS) && USE(ACCELERATED_COMPOSITING) && USE(GLES2_RENDERING)
> +
> +#include "Canvas2DLayerChromium.h"
> +
> +#include "CanvasRenderingContext2D.h"
> +#include "GLES2Context.h"
> +#include "GraphicsContext.h"
> +#include "HTMLCanvasElement.h"
> +#include "PlatformContextSkia.h"
> +#include <GLES2/gl2.h>
> +
> +namespace WebCore {
> +
> +unsigned Canvas2DLayerChromium::m_shaderProgramId = 0;
> +
> +PassRefPtr<Canvas2DLayerChromium> Canvas2DLayerChromium::create(CanvasRenderingContext2D* context)
> +{
> +    return adoptRef(new Canvas2DLayerChromium(context));
> +}
> +
> +Canvas2DLayerChromium::Canvas2DLayerChromium(CanvasRenderingContext2D* context)
> +    : LayerChromium(0)
> +    , m_context(context)
> +    , m_textureChanged(true)
> +{
> +}
> +
> +unsigned Canvas2DLayerChromium::textureId()
> +{
> +    return m_context->gles2Context()->getOffscreenContentParentTextureId();
> +}
> +
> +void Canvas2DLayerChromium::updateTextureContents(unsigned textureId)
> +{
> +    ASSERT(textureId == m_context->gles2Context()->getOffscreenContentParentTextureId());
> +    if (m_textureChanged) {
> +        glBindTexture(GL_TEXTURE_2D, textureId);
> +        // Set the min-mag filters to linear and wrap modes to GL_CLAMP_TO_EDGE
> +        // to get around NPOT texture limitations of GLES.
> +        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
> +        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
> +        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
> +        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
> +        m_textureChanged = false;
> +    }
> +    // Update the contents of the texture used by the compositor.
> +    if (m_contentsDirty) {
> +        // Flush any software updates to the backing texture.
> +        m_context->canvas()->drawingContext()->platformContext()->prepareForHardwareDraw();
> +        m_context->gles2Context()->swapBuffers();
> +        m_contentsDirty = false;
> +    }
> +}
> +
> +}
> +#endif // ENABLE(ACCELERATED_2D_CANVAS) && USE(ACCELERATED_COMPOSITING) && USE(GLES2_RENDERING)
> diff --git a/WebCore/platform/graphics/chromium/Canvas2DLayerChromium.h b/WebCore/platform/graphics/chromium/Canvas2DLayerChromium.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..a94e5c13110e937a20632ea9aefd6ef0fdc2ea4f
> --- /dev/null
> +++ b/WebCore/platform/graphics/chromium/Canvas2DLayerChromium.h
> @@ -0,0 +1,66 @@
> +/*
> + * Copyright (C) 2010 Google Inc. All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions are
> + * met:
> + *
> + *     * Redistributions of source code must retain the above copyright
> + * notice, this list of conditions and the following disclaimer.
> + *     * Redistributions in binary form must reproduce the above
> + * copyright notice, this list of conditions and the following disclaimer
> + * in the documentation and/or other materials provided with the
> + * distribution.
> + *     * Neither the name of Google Inc. nor the names of its
> + * contributors may be used to endorse or promote products derived from
> + * this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
> + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
> + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
> + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +
> +#ifndef Canvas2DLayerChromium_h
> +#define Canvas2DLayerChromium_h
> +
> +#if USE(ACCELERATED_COMPOSITING) && ENABLE(ACCELERATED_2D_CANVAS) && USE(GLES2_RENDERING)
> +
> +#include "LayerChromium.h"
> +
> +namespace WebCore {
> +
> +class CanvasRenderingContext2D;
> +
> +// A Layer that contains a Canvas2D element.
> +class Canvas2DLayerChromium : public LayerChromium {
> +public:
> +    static PassRefPtr<Canvas2DLayerChromium> create(CanvasRenderingContext2D*);
> +    virtual bool drawsContent() { return m_context; }
> +    virtual bool ownsTexture() { return true; }
> +    virtual void updateTextureContents(unsigned);
> +    virtual unsigned textureId();
> +    virtual unsigned shaderProgramId() { return m_shaderProgramId; }
> +
> +    static void setShaderProgramId(unsigned shaderProgramId) { m_shaderProgramId = shaderProgramId; }
> +
> +private:
> +    explicit Canvas2DLayerChromium(CanvasRenderingContext2D*);
> +    CanvasRenderingContext2D* m_context;
> +    bool m_textureChanged;
> +
> +    static unsigned m_shaderProgramId;
> +};
> +
> +}
> +#endif // USE(ACCELERATED_COMPOSITING)
> +
> +#endif
> diff --git a/WebCore/platform/graphics/chromium/GraphicsLayerChromium.cpp b/WebCore/platform/graphics/chromium/GraphicsLayerChromium.cpp
> index a01a17fd2a5984f682a908ebae5b870685c0717e..bdb0581a1b93148371017858a4b55ad7af61b075 100644
> --- a/WebCore/platform/graphics/chromium/GraphicsLayerChromium.cpp
> +++ b/WebCore/platform/graphics/chromium/GraphicsLayerChromium.cpp
> @@ -45,6 +45,7 @@
>  
>  #include "GraphicsLayerChromium.h"
>  
> +#include "Canvas2DLayerChromium.h"
>  #include "FloatConversion.h"
>  #include "FloatRect.h"
>  #include "Image.h"
> @@ -363,6 +364,36 @@ void GraphicsLayerChromium::setContentsToWebGL(PlatformLayer* platformLayer)
>  }
>  #endif
>  
> +#if ENABLE(ACCELERATED_2D_CANVAS)
> +void GraphicsLayerChromium::setContentsToAccelerated2DCanvas(PlatformLayer* platformLayer)
> +{
> +    bool childrenChanged = false;
> +    if (platformLayer) {
> +        // Must change the owner now, otherwise setupContentsLayer() below may reference a garbage m_owner.
> +        platformLayer->setOwner(this);
> +        if (!m_contentsLayer.get() || m_contentsLayerPurpose != ContentsLayerForAccelerated2DCanvas) {
> +            Canvas2DLayerChromium* canvas2DLayer = static_cast<Canvas2DLayerChromium*>(platformLayer);
> +            setupContentsLayer(canvas2DLayer);
> +            m_contentsLayer = canvas2DLayer;
> +            m_contentsLayerPurpose = ContentsLayerForAccelerated2DCanvas;
> +            childrenChanged = true;
> +        }
> +        platformLayer->setNeedsDisplay();
> +        updateContentsRect();
> +    } else {
> +        if (m_contentsLayer) {
> +            childrenChanged = true;
> +
> +            // The old contents layer will be removed via updateSublayerList.
> +            m_contentsLayer = 0;
> +        }
> +    }
> +
> +    if (childrenChanged)
> +        updateSublayerList();
> +}
> +#endif
> +
>  void GraphicsLayerChromium::setContentsToMedia(PlatformLayer* layer)
>  {
>      bool childrenChanged = false;
> diff --git a/WebCore/platform/graphics/chromium/GraphicsLayerChromium.h b/WebCore/platform/graphics/chromium/GraphicsLayerChromium.h
> index cd5e4799ab4f031a41831200e8a58d27920e3420..1dca68a66cdcc0cea388fb6c275bb34305fd73fb 100644
> --- a/WebCore/platform/graphics/chromium/GraphicsLayerChromium.h
> +++ b/WebCore/platform/graphics/chromium/GraphicsLayerChromium.h
> @@ -87,6 +87,9 @@ public:
>      virtual void setContentsToImage(Image*);
>      virtual void setContentsToMedia(PlatformLayer*);
>      virtual void setContentsToWebGL(PlatformLayer*);
> +#if ENABLE(ACCELERATED_2D_CANVAS)
> +    virtual void setContentsToAccelerated2DCanvas(PlatformLayer*);
> +#endif
>  
>      virtual PlatformLayer* platformLayer() const;
>  
> @@ -137,7 +140,8 @@ private:
>          NoContentsLayer = 0,
>          ContentsLayerForImage,
>          ContentsLayerForVideo,
> -        ContentsLayerForWebGL
> +        ContentsLayerForWebGL,
> +        ContentsLayerForAccelerated2DCanvas
>      };
>  
>      ContentsLayerPurpose m_contentsLayerPurpose;
> diff --git a/WebCore/platform/graphics/chromium/LayerRendererChromium.cpp b/WebCore/platform/graphics/chromium/LayerRendererChromium.cpp
> index b0713853975e8d38d47dd59bc79a484fa1d28bc7..47485bfdcf5f95fc4bdd8f7ba1ddf41a8007690a 100644
> --- a/WebCore/platform/graphics/chromium/LayerRendererChromium.cpp
> +++ b/WebCore/platform/graphics/chromium/LayerRendererChromium.cpp
> @@ -34,6 +34,7 @@
>  #if USE(ACCELERATED_COMPOSITING)
>  #include "LayerRendererChromium.h"
>  
> +#include "Canvas2DLayerChromium.h"
>  #include "GLES2Context.h"
>  #include "LayerChromium.h"
>  #include "NotImplemented.h"
> @@ -623,6 +624,7 @@ void LayerRendererChromium::drawLayer(LayerChromium* layer)
>          if (layer->contentsDirty()) {
>              // Update the backing texture contents for any dirty portion of the layer.
>              layer->updateTextureContents(textureId);
> +            m_gles2Context->makeCurrent();
>          }
>  
>          if (layer->doubleSided())
> @@ -746,6 +748,9 @@ bool LayerRendererChromium::initializeSharedGLObjects()
>          return false;
>      }
>      WebGLLayerChromium::setShaderProgramId(WebGLLayerProgram);
> +#if ENABLE(ACCELERATED_2D_CANVAS)
> +    Canvas2DLayerChromium::setShaderProgramId(WebGLLayerProgram);
> +#endif
>  
>      if (!createLayerShader(ScrollLayerProgram, vertexShaderString, scrollFragmentShaderString)) {
>          LOG_ERROR("Failed to create shader program for scrolling layer");

WebCore/platform/graphics/chromium/GraphicsLayerChromium.cpp:391
 +  
Once https://bugs.webkit.org/show_bug.cgi?id=43291 lands then you'll need to add the following lines here:

    // If we have a contents layer then the main layer becomes just a container
    // and therefore should not render anything.
    if (m_layer)
        m_layer->setSkipDraw(platformLayer);

to make sure that the software platform layer that's still associated with the canvas doesn't update as well.
Comment 3 Ojan Vafai 2010-08-03 12:02:51 PDT
Comment on attachment 63231 [details]
Patch

Whoops. Didn't mean to r+ that. This needs a ChangeLog. Also, is there a way to test any of this?
Comment 4 James Robinson 2010-08-03 12:27:36 PDT
Whoops, forgot about the changelog.  This needs to be updated pending feedback on another bug, I'll update when the dust settles.
Comment 5 James Robinson 2010-09-27 11:35:03 PDT
Obsolete now.