⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 283362 in webkit


Ignore:
Timestamp:
Oct 1, 2021, 5:53:49 AM (5 years ago)
Author:
commit-queue@webkit.org
Message:

Cocoa GraphicsContextGLOpenGL should not be used by GraphicsContextGLCVANGLE
https://bugs.webkit.org/show_bug.cgi?id=231010

Patch by Kimmo Kinnunen <kkinnunen@apple.com> on 2021-10-01
Reviewed by Kenneth Russell.

GraphicsContextGLOpenGL creates a GraphicsContextGLCVANGLE helper to do
do YUV texture uploads. This GraphicsContextGLCVANGLE creates a OpenGL ES context that
shares the OpenGL state by the caller.

Previously the new context would be a GraphicsContextGLOpenGL. This would be a problem
since GraphicsContextGLOpenGL has compositor related logic and objects, for example
the WebGL layer as well as resources for default framebuffer operation. Shared context
would also go to the GraphicsContextGLOpenGLManager context list even though
they could not be deleted behind the owners back during context recycling.

Instead, create just a normal ANGLE context and call ANGLE directly from
GraphicsContextGLCVANGLE. This is more consistent with software layers:
GraphicsContextGLOpenGL and GraphicsContextGLCVANGLE are at the same software
level, part of the same GraphicsContextGLOpenGL implementation.

Makes it possible to remove otherwise unused and unfitting "shared context"
related functionality from WebGL level from GraphicsContextGL.

No new tests, refactor.

  • SourcesCocoa.txt:
  • WebCore.xcodeproj/project.pbxproj:
  • platform/graphics/GraphicsContextGL.cpp:

(WebCore::GraphicsContextGL::GraphicsContextGL):

  • platform/graphics/GraphicsContextGL.h:
  • platform/graphics/angle/GraphicsContextGLANGLE.cpp:

(WebCore::GraphicsContextGLOpenGL::asCV): Deleted.

  • platform/graphics/cocoa/ANGLEUtilitiesCocoa.cpp: Added.

(WebCore::createPbufferAndAttachIOSurface):
(WebCore::destroyPbufferAndDetachIOSurface):

  • platform/graphics/cocoa/ANGLEUtilitiesCocoa.h: Added.

Move the createPbufferAndAttachIOSurface to Cocoa specific
ANGLE utility file.
See also bug 226504 - Adopt createPbufferAndAttachIOSurface/destroyPbufferAndDetachIOSurface
in GraphicsContextGLCVANGLE.cpp and GraphicsContextGLOpenGLCocoa.mm

  • platform/graphics/cocoa/GraphicsContextGLOpenGLCocoa.mm:

(WebCore::GraphicsContextGLOpenGL::create):
(WebCore::GraphicsContextGLOpenGL::createForGPUProcess):
(WebCore::GraphicsContextGLOpenGL::GraphicsContextGLOpenGL):
(WebCore::GraphicsContextGLOpenGL::~GraphicsContextGLOpenGL):
(WebCore::GraphicsContextGLOpenGL::checkGPUStatus):
(WebCore::GraphicsContextGLOpenGL::makeCurrent):
Add a new function to make the EGL context current and maintain
the GraphicsContextGLOpenGL::currentContext cache value correctly.
Cache needs to be invalidated always when EGL context changes.
The cache value is reset always, and then set only when needed
during GraphicsContextGLOpenGL::makeContextCurrent().

Make GraphicsContextGLCVANGLE friend of GraphicsContextGLOpenGL, since
essentially the former is part of the implementation of latter.

(WebCore::GraphicsContextGLOpenGL::createPbufferAndAttachIOSurface):
(WebCore::GraphicsContextGLOpenGL::destroyPbufferAndDetachIOSurface):
Move the implementation to a ANGLE related helper files above.

(WebCore::GraphicsContextGLOpenGL::asCV):
Move the asCV to the implementaton file for Cococa, as that is
the platform which uses the implementation.

(WebCore::GraphicsContextGLOpenGL::createShared): Deleted.
(WebCore::GraphicsContextGLOpenGL::clearCurrentContext): Deleted.
Replace with GraphicsContextGLOpenGL::makeCurrent().

  • platform/graphics/cv/GraphicsContextGLCVANGLE.cpp:

(WebCore::YCbCrMatrix::operator const GLfloat* const):
(WebCore::YCbCrToRGBMatrixForRangeAndTransferFunction):
(WebCore::GraphicsContextGLCVANGLE::create):
(WebCore::GraphicsContextGLCVANGLE::~GraphicsContextGLCVANGLE):
(WebCore::GraphicsContextGLCVANGLE::GraphicsContextGLCVANGLE):
Reorder the resource initialization with ScopeExit cleanup functions.
Avoid storing the yuvProgram, instead just use the program as program
binary and delete the program.

(WebCore::GraphicsContextGLCVANGLE::copyPixelBufferToTexture):
(WebCore::YCbCrMatrix::operator GCGLSpan<const GLfloat, 16> const): Deleted.
(WebCore::GraphicsContextGLCVANGLE::initializeUVContextObjects): Deleted.
Move the resource initialization into the constructor. The object
itself is already initialized only on demand.
(WebCore::GraphicsContextGLCVANGLE::attachIOSurfaceToTexture): Deleted.
(WebCore::GraphicsContextGLCVANGLE::detachIOSurfaceFromTexture): Deleted.
Share the implementation with the existing implementation from
WebCore::GraphicsContextGLOpenGL::createPbufferAndAttachIOSurface that
was moved to WebCore::createPbufferAndAttachIOSurface.

  • platform/graphics/cv/GraphicsContextGLCVANGLE.h:

Remove the m_yuvProgram, rather use just the program binary.

  • platform/graphics/opengl/GraphicsContextGLOpenGL.h:
  • platform/graphics/texmap/GraphicsContextGLTextureMapper.cpp:

(WebCore::GraphicsContextGLOpenGL::GraphicsContextGLOpenGL):
Remove the redundant sharedContext parameter.

Location:
trunk/Source/WebCore
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r283361 r283362  
     12021-10-01  Kimmo Kinnunen  <kkinnunen@apple.com>
     2
     3        Cocoa GraphicsContextGLOpenGL should not be used by GraphicsContextGLCVANGLE
     4        https://bugs.webkit.org/show_bug.cgi?id=231010
     5
     6        Reviewed by Kenneth Russell.
     7
     8        GraphicsContextGLOpenGL creates a GraphicsContextGLCVANGLE helper to do
     9        do YUV texture uploads. This GraphicsContextGLCVANGLE creates a OpenGL ES context that
     10        shares the OpenGL state by the caller.
     11
     12        Previously the new context would be a GraphicsContextGLOpenGL. This would be a problem
     13        since GraphicsContextGLOpenGL has compositor related logic and objects, for example
     14        the WebGL layer as well as resources for default framebuffer operation. Shared context
     15        would also go to the GraphicsContextGLOpenGLManager context list even though
     16        they could not be deleted behind the owners back during context recycling.
     17
     18        Instead, create just a normal ANGLE context and call ANGLE directly from
     19        GraphicsContextGLCVANGLE. This is more consistent with software layers:
     20        GraphicsContextGLOpenGL and GraphicsContextGLCVANGLE are at the same software
     21        level, part of the same GraphicsContextGLOpenGL implementation.
     22
     23        Makes it possible to remove otherwise unused and unfitting "shared context"
     24        related functionality from WebGL level from GraphicsContextGL.
     25
     26        No new tests, refactor.
     27
     28        * SourcesCocoa.txt:
     29        * WebCore.xcodeproj/project.pbxproj:
     30        * platform/graphics/GraphicsContextGL.cpp:
     31        (WebCore::GraphicsContextGL::GraphicsContextGL):
     32        * platform/graphics/GraphicsContextGL.h:
     33        * platform/graphics/angle/GraphicsContextGLANGLE.cpp:
     34        (WebCore::GraphicsContextGLOpenGL::asCV): Deleted.
     35        * platform/graphics/cocoa/ANGLEUtilitiesCocoa.cpp: Added.
     36        (WebCore::createPbufferAndAttachIOSurface):
     37        (WebCore::destroyPbufferAndDetachIOSurface):
     38        * platform/graphics/cocoa/ANGLEUtilitiesCocoa.h: Added.
     39        Move the createPbufferAndAttachIOSurface to Cocoa specific
     40        ANGLE utility file.
     41        See also bug 226504 - Adopt createPbufferAndAttachIOSurface/destroyPbufferAndDetachIOSurface
     42        in GraphicsContextGLCVANGLE.cpp and GraphicsContextGLOpenGLCocoa.mm
     43
     44        * platform/graphics/cocoa/GraphicsContextGLOpenGLCocoa.mm:
     45        (WebCore::GraphicsContextGLOpenGL::create):
     46        (WebCore::GraphicsContextGLOpenGL::createForGPUProcess):
     47        (WebCore::GraphicsContextGLOpenGL::GraphicsContextGLOpenGL):
     48        (WebCore::GraphicsContextGLOpenGL::~GraphicsContextGLOpenGL):
     49        (WebCore::GraphicsContextGLOpenGL::checkGPUStatus):
     50        (WebCore::GraphicsContextGLOpenGL::makeCurrent):
     51        Add a new function to make the EGL context current and maintain
     52        the GraphicsContextGLOpenGL::currentContext cache value correctly.
     53        Cache needs to be invalidated always when EGL context changes.
     54        The cache value is reset always, and then set only when needed
     55        during GraphicsContextGLOpenGL::makeContextCurrent().
     56
     57        Make GraphicsContextGLCVANGLE friend of GraphicsContextGLOpenGL, since
     58        essentially the former is part of the implementation of latter.
     59
     60        (WebCore::GraphicsContextGLOpenGL::createPbufferAndAttachIOSurface):
     61        (WebCore::GraphicsContextGLOpenGL::destroyPbufferAndDetachIOSurface):
     62        Move the implementation to a ANGLE related helper files above.
     63
     64        (WebCore::GraphicsContextGLOpenGL::asCV):
     65        Move the asCV to the implementaton file for Cococa, as that is
     66        the platform which uses the implementation.
     67
     68        (WebCore::GraphicsContextGLOpenGL::createShared): Deleted.
     69        (WebCore::GraphicsContextGLOpenGL::clearCurrentContext): Deleted.
     70        Replace with GraphicsContextGLOpenGL::makeCurrent().
     71
     72        * platform/graphics/cv/GraphicsContextGLCVANGLE.cpp:
     73        (WebCore::YCbCrMatrix::operator const GLfloat* const):
     74        (WebCore::YCbCrToRGBMatrixForRangeAndTransferFunction):
     75        (WebCore::GraphicsContextGLCVANGLE::create):
     76        (WebCore::GraphicsContextGLCVANGLE::~GraphicsContextGLCVANGLE):
     77        (WebCore::GraphicsContextGLCVANGLE::GraphicsContextGLCVANGLE):
     78        Reorder the resource initialization with ScopeExit cleanup functions.
     79        Avoid storing the yuvProgram, instead just use the program as program
     80        binary and delete the program.
     81
     82        (WebCore::GraphicsContextGLCVANGLE::copyPixelBufferToTexture):
     83        (WebCore::YCbCrMatrix::operator GCGLSpan<const GLfloat, 16> const): Deleted.
     84        (WebCore::GraphicsContextGLCVANGLE::initializeUVContextObjects): Deleted.
     85        Move the resource initialization into the constructor. The object
     86        itself is already initialized only on demand.
     87        (WebCore::GraphicsContextGLCVANGLE::attachIOSurfaceToTexture): Deleted.
     88        (WebCore::GraphicsContextGLCVANGLE::detachIOSurfaceFromTexture): Deleted.
     89        Share the implementation with the existing implementation from
     90        WebCore::GraphicsContextGLOpenGL::createPbufferAndAttachIOSurface that
     91        was moved to WebCore::createPbufferAndAttachIOSurface.
     92
     93        * platform/graphics/cv/GraphicsContextGLCVANGLE.h:
     94        Remove the m_yuvProgram, rather use just the program binary.
     95
     96        * platform/graphics/opengl/GraphicsContextGLOpenGL.h:
     97        * platform/graphics/texmap/GraphicsContextGLTextureMapper.cpp:
     98        (WebCore::GraphicsContextGLOpenGL::GraphicsContextGLOpenGL):
     99        Remove the redundant sharedContext parameter.
     100
    11012021-10-01  Carlos Garcia Campos  <cgarcia@igalia.com>
    2102
  • trunk/Source/WebCore/SourcesCocoa.txt

    r283217 r283362  
    360360platform/graphics/cg/TransformationMatrixCG.cpp
    361361platform/graphics/cg/UTIRegistry.cpp
     362platform/graphics/cocoa/ANGLEUtilitiesCocoa.cpp
    362363platform/graphics/cocoa/AudioTrackPrivateWebM.cpp
    363364platform/graphics/cocoa/ColorCocoa.mm
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r283347 r283362  
    1064810648                7BB680B725BA1BE3002B8738 /* GraphicsChecksMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GraphicsChecksMac.cpp; sourceTree = "<group>"; };
    1064910649                7BB680B825BA1BE4002B8738 /* GraphicsChecksMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsChecksMac.h; sourceTree = "<group>"; };
     10650                7BCD42DA2705C94900EB2127 /* ANGLEUtilitiesCocoa.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ANGLEUtilitiesCocoa.cpp; sourceTree = "<group>"; };
     10651                7BCD42DC2705C94900EB2127 /* ANGLEUtilitiesCocoa.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ANGLEUtilitiesCocoa.h; sourceTree = "<group>"; };
    1065010652                7BE7265B25763B8D00E85D98 /* RemoteGraphicsContextGLProxyBase.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RemoteGraphicsContextGLProxyBase.cpp; sourceTree = "<group>"; };
    1065110653                7C011F3D24FAD360005BEF10 /* Settings.cpp.erb */ = {isa = PBXFileReference; lastKnownFileType = text; path = Settings.cpp.erb; sourceTree = "<group>"; };
     
    2733127333                        isa = PBXGroup;
    2733227334                        children = (
     27335                                7BCD42DA2705C94900EB2127 /* ANGLEUtilitiesCocoa.cpp */,
     27336                                7BCD42DC2705C94900EB2127 /* ANGLEUtilitiesCocoa.h */,
    2733327337                                CDF9950124C28149002EA062 /* AudioTrackPrivateWebM.cpp */,
    2733427338                                CDF9950024C28149002EA062 /* AudioTrackPrivateWebM.h */,
     
    3140231406                                27E3C808257F5E6E00C986AB /* ANGLEHeaders.h in Headers */,
    3140331407                                31A795C71888BCB200382F90 /* ANGLEInstancedArrays.h in Headers */,
     31408                                7BB34A1725345CB200029D08 /* ANGLEUtilities.h in Headers */,
    3140431409                                49E912AB0EFAC906009D0CAF /* Animation.h in Headers */,
    3140531410                                71EFCEDC202B38A900D7C411 /* AnimationEffect.h in Headers */,
     
    3264932654                                313DE87023A96973008FC47B /* GraphicsContextGL.h in Headers */,
    3265032655                                7B45AB5525FBA9DE00FD27F4 /* GraphicsContextGLANGLEEGLUtilities.h in Headers */,
    32651                                 7BB34A1725345CB200029D08 /* GraphicsContextGLANGLEUtilities.h in Headers */,
    3265232656                                7C330A021DF8FAC600D3395C /* GraphicsContextGLAttributes.h in Headers */,
    3265332657                                7B10339E2549721700C8C1AC /* GraphicsContextGLCV.h in Headers */,
  • trunk/Source/WebCore/platform/graphics/GraphicsContextGL.cpp

    r283238 r283362  
    358358}
    359359
    360 GraphicsContextGL::GraphicsContextGL(GraphicsContextGLAttributes attrs, GraphicsContextGL*)
     360GraphicsContextGL::GraphicsContextGL(GraphicsContextGLAttributes attrs)
    361361    : m_attrs(attrs)
    362362{
  • trunk/Source/WebCore/platform/graphics/GraphicsContextGL.h

    r283238 r283362  
    915915    WEBCORE_EXPORT static RefPtr<GraphicsContextGL> create(const GraphicsContextGLAttributes&, HostWindow*);
    916916
    917     GraphicsContextGL(GraphicsContextGLAttributes, GraphicsContextGL* sharedContext = nullptr);
     917    GraphicsContextGL(GraphicsContextGLAttributes);
    918918    virtual ~GraphicsContextGL() = default;
    919919
  • trunk/Source/WebCore/platform/graphics/angle/GraphicsContextGLANGLE.cpp

    r281613 r283362  
    28432843}
    28442844
    2845 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
    2846 GraphicsContextGLCV* GraphicsContextGLOpenGL::asCV()
    2847 {
    2848     if (!m_cv)
    2849         m_cv = makeUnique<GraphicsContextGLCVANGLE>(*this);
    2850     return m_cv.get();
    2851 }
    2852 #endif
    2853 
    28542845bool GraphicsContextGLOpenGL::waitAndUpdateOldestFrame()
    28552846{
  • trunk/Source/WebCore/platform/graphics/cocoa/GraphicsContextGLOpenGLCocoa.mm

    r283308 r283362  
    2929#import "GraphicsContextGLOpenGL.h"
    3030
     31#import "ANGLEUtilitiesCocoa.h"
    3132#import "CVUtilities.h"
    3233#import "ExtensionsGLANGLE.h"
     
    4950
    5051#if ENABLE(VIDEO) && USE(AVFOUNDATION)
    51 #include "GraphicsContextGLCV.h"
     52#include "GraphicsContextGLCVANGLE.h"
    5253#endif
    5354
     
    198199        return nullptr;
    199200
    200     RefPtr<GraphicsContextGLOpenGL> context = adoptRef(new GraphicsContextGLOpenGL(attrs, hostWindow, nullptr, nullptr));
     201    RefPtr<GraphicsContextGLOpenGL> context = adoptRef(new GraphicsContextGLOpenGL(attrs, hostWindow, nullptr));
    201202
    202203    if (!context->m_contextObj)
     
    208209}
    209210
    210 Ref<GraphicsContextGLOpenGL> GraphicsContextGLOpenGL::createShared(GraphicsContextGLOpenGL& sharedContext)
    211 {
    212 
    213     auto context = adoptRef(*new GraphicsContextGLOpenGL(sharedContext.contextAttributes(), nullptr, &sharedContext, nullptr));
    214 
    215     GraphicsContextGLOpenGLManager::sharedManager().addContext(context.ptr());
    216 
    217     return context;
    218 }
    219 
    220211Ref<GraphicsContextGLOpenGL> GraphicsContextGLOpenGL::createForGPUProcess(const GraphicsContextGLAttributes& attrs, GraphicsContextGLIOSurfaceSwapChain* swapChain)
    221212{
    222     return adoptRef(*new GraphicsContextGLOpenGL(attrs, nullptr, nullptr, swapChain));
    223 }
    224 
    225 GraphicsContextGLOpenGL::GraphicsContextGLOpenGL(GraphicsContextGLAttributes attrs, HostWindow*, GraphicsContextGLOpenGL* sharedContext, GraphicsContextGLIOSurfaceSwapChain* swapChain)
    226     : GraphicsContextGL(attrs, sharedContext)
     213    return adoptRef(*new GraphicsContextGLOpenGL(attrs, nullptr, swapChain));
     214}
     215
     216GraphicsContextGLOpenGL::GraphicsContextGLOpenGL(GraphicsContextGLAttributes attrs, HostWindow*, GraphicsContextGLIOSurfaceSwapChain* swapChain)
     217    : GraphicsContextGL(attrs)
    227218{
    228219    m_isForWebGL2 = attrs.webGLVersion == GraphicsContextGLWebGLVersion::WebGL2;
     
    293284        eglContextAttributes.append(EGL_FALSE);
    294285    }
    295     if (!sharedContext) {
    296         // The shared context is only non-null when creating a context
    297         // on behalf of the VideoTextureCopier. WebGL-specific rendering
    298         // feedback loop validation does not work in multi-context
    299         // scenarios, and must be disabled for the VideoTextureCopier's
    300         // context.
    301         eglContextAttributes.append(EGL_CONTEXT_WEBGL_COMPATIBILITY_ANGLE);
    302         eglContextAttributes.append(EGL_TRUE);
    303         // WebGL requires that all resources are cleared at creation.
    304         // FIXME: performing robust resource initialization in the VideoTextureCopier adds a large amount of overhead
    305         // so it would be nice to avoid that there (we should always be touching every pixel as we copy).
    306         eglContextAttributes.append(EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE);
    307         eglContextAttributes.append(EGL_TRUE);
    308     }
     286    eglContextAttributes.append(EGL_CONTEXT_WEBGL_COMPATIBILITY_ANGLE);
     287    eglContextAttributes.append(EGL_TRUE);
     288
     289    // WebGL requires that all resources are cleared at creation.
     290    eglContextAttributes.append(EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE);
     291    eglContextAttributes.append(EGL_TRUE);
    309292
    310293    // WebGL doesn't allow client arrays.
     
    317300    eglContextAttributes.append(EGL_NONE);
    318301
    319     m_contextObj = EGL_CreateContext(m_displayObj, m_configObj, sharedContext ? static_cast<EGLContext>(sharedContext->m_contextObj) : EGL_NO_CONTEXT, eglContextAttributes.data());
    320     if (m_contextObj == EGL_NO_CONTEXT) {
     302    m_contextObj = EGL_CreateContext(m_displayObj, m_configObj, EGL_NO_CONTEXT, eglContextAttributes.data());
     303    if (m_contextObj == EGL_NO_CONTEXT || !makeCurrent(m_displayObj, m_contextObj)) {
    321304        LOG(WebGL, "EGLContext Initialization failed.");
    322305        return;
    323306    }
    324307    LOG(WebGL, "Got EGLContext");
    325 
    326     EGL_MakeCurrent(m_displayObj, EGL_NO_SURFACE, EGL_NO_SURFACE, m_contextObj);
    327308
    328309    if (m_isForWebGL2)
     
    464445    }
    465446    if (m_contextObj) {
    466         clearCurrentContext();
     447        makeCurrent(m_displayObj, EGL_NO_CONTEXT);
    467448        EGL_DestroyContext(m_displayObj, m_contextObj);
    468449    }
     
    528509}
    529510
    530 void GraphicsContextGLOpenGL::clearCurrentContext()
    531 {
    532     EGLBoolean result = EGL_MakeCurrent(m_displayObj, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    533     ASSERT_UNUSED(result, result);
    534     currentContext = nullptr;
    535 }
    536 
    537511#if PLATFORM(IOS_FAMILY)
    538512bool GraphicsContextGLOpenGL::releaseCurrentContext(ReleaseBehavior releaseBehavior)
     
    580554        m_failNextStatusCheck = false;
    581555        forceContextLost();
    582         clearCurrentContext();
     556        makeCurrent(m_displayObj, EGL_NO_CONTEXT);
    583557        return;
    584558    }
     
    681655}
    682656
     657bool GraphicsContextGLOpenGL::makeCurrent(PlatformGraphicsContextGLDisplay display, PlatformGraphicsContextGL context)
     658{
     659    currentContext = nullptr;
     660    return EGL_MakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
     661}
     662
    683663void* GraphicsContextGLOpenGL::createPbufferAndAttachIOSurface(GCGLenum target, PbufferAttachmentUsage usage, GCGLenum internalFormat, GCGLsizei width, GCGLsizei height, GCGLenum type, IOSurfaceRef surface, GCGLuint plane)
    684664{
    685     if (target != GraphicsContextGL::TEXTURE_RECTANGLE_ARB && target != GraphicsContextGL::TEXTURE_2D) {
     665    if (target != GraphicsContextGLOpenGL::drawingBufferTextureTarget()) {
    686666        LOG(WebGL, "Unknown texture target %d.", static_cast<int>(target));
    687667        return nullptr;
    688668    }
    689669
    690     auto eglTextureTarget = [&] () -> EGLint {
    691         if (target == GraphicsContextGL::TEXTURE_RECTANGLE_ARB)
    692             return EGL_TEXTURE_RECTANGLE_ANGLE;
    693         return EGL_TEXTURE_2D;
    694     }();
    695 
    696     if (eglTextureTarget != GraphicsContextGLOpenGL::EGLDrawingBufferTextureTarget()) {
    697         LOG(WebGL, "Mismatch in EGL texture target: %d should be %d.", static_cast<int>(target), GraphicsContextGLOpenGL::EGLDrawingBufferTextureTarget());
    698         return nullptr;
    699     }
    700 
    701     auto usageHintAngle = [&] () -> EGLint {
     670    auto usageHint = [&] () -> EGLint {
    702671        if (usage == PbufferAttachmentUsage::Read)
    703672            return EGL_IOSURFACE_READ_HINT_ANGLE;
     
    707676    }();
    708677
    709     const EGLint surfaceAttributes[] = {
    710         EGL_WIDTH, width,
    711         EGL_HEIGHT, height,
    712         EGL_IOSURFACE_PLANE_ANGLE, static_cast<EGLint>(plane),
    713         EGL_TEXTURE_TARGET, static_cast<EGLint>(eglTextureTarget),
    714         EGL_TEXTURE_INTERNAL_FORMAT_ANGLE, static_cast<EGLint>(internalFormat),
    715         EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,
    716         EGL_TEXTURE_TYPE_ANGLE, static_cast<EGLint>(type),
    717         // Only has an effect on the iOS Simulator.
    718         EGL_IOSURFACE_USAGE_HINT_ANGLE, usageHintAngle,
    719         EGL_NONE, EGL_NONE
    720     };
    721 
    722     auto display = platformDisplay();
    723     EGLSurface pbuffer = EGL_CreatePbufferFromClientBuffer(display, EGL_IOSURFACE_ANGLE, surface, platformConfig(), surfaceAttributes);
    724     if (!pbuffer) {
    725         LOG(WebGL, "EGL_CreatePbufferFromClientBuffer failed.");
    726         return nullptr;
    727     }
    728 
    729     if (!EGL_BindTexImage(display, pbuffer, EGL_BACK_BUFFER)) {
    730         LOG(WebGL, "EGL_BindTexImage failed.");
    731         EGL_DestroySurface(display, pbuffer);
    732         return nullptr;
    733     }
    734 
    735     return pbuffer;
     678    return WebCore::createPbufferAndAttachIOSurface(m_displayObj, m_configObj, target, usageHint, internalFormat, width, height, type, surface, plane);
    736679}
    737680
    738681void GraphicsContextGLOpenGL::destroyPbufferAndDetachIOSurface(void* handle)
    739682{
    740     auto display = platformDisplay();
    741     EGL_ReleaseTexImage(display, handle, EGL_BACK_BUFFER);
    742     EGL_DestroySurface(display, handle);
     683    WebCore::destroyPbufferAndDetachIOSurface(m_displayObj, handle);
    743684}
    744685
     
    845786    }
    846787}
     788
     789
     790#if ENABLE(VIDEO) && USE(AVFOUNDATION)
     791GraphicsContextGLCV* GraphicsContextGLOpenGL::asCV()
     792{
     793    if (!m_cv)
     794        m_cv = GraphicsContextGLCVANGLE::create(*this);
     795    return m_cv.get();
     796}
     797#endif
    847798
    848799std::optional<PixelBuffer> GraphicsContextGLOpenGL::readCompositedResults()
  • trunk/Source/WebCore/platform/graphics/cv/GraphicsContextGLCVANGLE.cpp

    r281791 r283362  
    2929#if ENABLE(WEBGL) && ENABLE(VIDEO) && USE(AVFOUNDATION)
    3030
    31 #include "ANGLEHeaders.h"
     31#include "ANGLEUtilitiesCocoa.h"
    3232#include "FourCC.h"
     33#include "GraphicsContextGLOpenGL.h"
    3334#include "Logging.h"
    3435#include <pal/spi/cf/CoreVideoSPI.h>
    3536#include <pal/spi/cocoa/IOSurfaceSPI.h>
    3637#include <wtf/NeverDestroyed.h>
     38#include <wtf/Scope.h>
    3739#include <wtf/StdMap.h>
    3840#include <wtf/cf/TypeCastsCF.h>
     
    231233    constexpr YCbCrMatrix(PixelRange, GLfloat cbCoefficient, GLfloat crCoefficient);
    232234
    233     operator GCGLSpan<const GLfloat, 16>() const
     235    operator const GLfloat*() const
    234236    {
    235         return makeGCGLSpan<16>(&rows[0][0]);
     237        return &rows[0][0];
    236238    }
    237239
     
    305307}
    306308
    307 static GCGLSpan<const GLfloat, 16> YCbCrToRGBMatrixForRangeAndTransferFunction(PixelRange range, TransferFunctionCV transferFunction)
     309static const GLfloat* YCbCrToRGBMatrixForRangeAndTransferFunction(PixelRange range, TransferFunctionCV transferFunction)
    308310{
    309311    using MapKey = std::pair<PixelRange, TransferFunctionCV>;
     
    431433}
    432434
    433 namespace {
    434 
    435 // Scoped holder of a cleanup function. Calls the function at the end of the scope.
    436 // Note: Releases the reference to the function only after the scope, not
    437 // at the time of `reset()` call.
    438 template <typename F>
    439 class ScopedCleanup {
    440 public:
    441     explicit ScopedCleanup(F&& function)
    442         : m_function(WTFMove(function))
    443     {
    444     }
    445     ~ScopedCleanup()
    446     {
    447         if (m_shouldCall)
    448             m_function();
    449     }
    450     void reset() { m_shouldCall = false; }
    451 private:
    452     bool m_shouldCall = true;
    453     const F m_function;
    454 };
    455 
    456 }
    457 
    458 GraphicsContextGLCVANGLE::GraphicsContextGLCVANGLE(GraphicsContextGLOpenGL& context)
    459     : m_context(GraphicsContextGLOpenGL::createShared(context))
    460     , m_framebuffer(m_context->createFramebuffer())
    461 {
     435std::unique_ptr<GraphicsContextGLCVANGLE> GraphicsContextGLCVANGLE::create(GraphicsContextGLOpenGL& context)
     436{
     437    std::unique_ptr<GraphicsContextGLCVANGLE> cv { new GraphicsContextGLCVANGLE(context) };
     438    if (!cv->m_context)
     439        return nullptr;
     440    return cv;
    462441}
    463442
    464443GraphicsContextGLCVANGLE::~GraphicsContextGLCVANGLE()
    465444{
    466     if (m_yuvVertexBuffer)
    467         m_context->deleteBuffer(m_yuvVertexBuffer);
    468     if (m_yuvProgram)
    469         m_context->deleteProgram(m_yuvProgram);
    470     m_context->deleteFramebuffer(m_framebuffer);
    471 }
    472 
    473 bool GraphicsContextGLCVANGLE::initializeUVContextObjects()
    474 {
    475     const bool useTexture2D = GraphicsContextGLOpenGL::drawingBufferTextureTarget() == GraphicsContextGL::TEXTURE_2D;
    476 
    477     PlatformGLObject vertexShader = m_context->createShader(GraphicsContextGL::VERTEX_SHADER);
    478     if (useTexture2D)
    479         m_context->shaderSource(vertexShader, s_yuvVertexShaderTexture2D);
    480     else
    481         m_context->shaderSource(vertexShader, s_yuvVertexShaderTextureRectangle);
    482 
    483     m_context->compileShaderDirect(vertexShader);
    484 
    485     GCGLint status = m_context->getShaderi(vertexShader, GraphicsContextGL::COMPILE_STATUS);
     445    if (!m_context || !GraphicsContextGLOpenGL::makeCurrent(m_display, m_context))
     446        return;
     447    gl::DeleteBuffers(1, &m_yuvVertexBuffer);
     448    gl::DeleteFramebuffers(1, &m_framebuffer);
     449    EGL_DestroyContext(m_display, m_context);
     450}
     451
     452GraphicsContextGLCVANGLE::GraphicsContextGLCVANGLE(GraphicsContextGLOpenGL& owner)
     453    : m_owner(owner)
     454{
     455    // Create compatible context that shares state with owner, but one that does not
     456    // have robustness or WebGL compatibility.
     457    const EGLint contextAttributes[] = {
     458        EGL_CONTEXT_CLIENT_VERSION,
     459        owner.m_isForWebGL2 ? 3 : 2,
     460        EGL_CONTEXT_OPENGL_BACKWARDS_COMPATIBLE_ANGLE,
     461        EGL_FALSE,
     462        EGL_CONTEXT_CLIENT_ARRAYS_ENABLED_ANGLE,
     463        EGL_FALSE,
     464        EGL_CONTEXT_BIND_GENERATES_RESOURCE_CHROMIUM,
     465        EGL_FALSE,
     466        EGL_NONE
     467    };
     468    EGLDisplay display = owner.platformDisplay();
     469    EGLConfig config = owner.platformConfig();
     470    EGLContext context = EGL_CreateContext(display, config, owner.m_contextObj, contextAttributes);
     471    if (context == EGL_NO_CONTEXT)
     472        return;
     473    GraphicsContextGLOpenGL::makeCurrent(display, context);
     474
     475    auto contextCleanup = makeScopeExit([display, context] {
     476        GraphicsContextGLOpenGL::makeCurrent(display, EGL_NO_CONTEXT);
     477        EGL_DestroyContext(display, context);
     478    });
     479
     480    const bool useTexture2D = GraphicsContextGLOpenGL::drawingBufferTextureTarget() == GL_TEXTURE_2D;
     481
     482#if PLATFORM(MAC) || PLATFORM(MACCATALYST)
     483    if (!useTexture2D) {
     484        gl::RequestExtensionANGLE("GL_ANGLE_texture_rectangle");
     485        gl::RequestExtensionANGLE("GL_EXT_texture_format_BGRA8888");
     486        if (gl::GetError() != GL_NO_ERROR)
     487            return;
     488    }
     489#endif
     490
     491    GLint vertexShader = gl::CreateShader(GL_VERTEX_SHADER);
     492    GLint fragmentShader = gl::CreateShader(GL_FRAGMENT_SHADER);
     493    GLuint yuvProgram = gl::CreateProgram();
     494    auto programCleanup = makeScopeExit([vertexShader, fragmentShader, yuvProgram] {
     495        gl::DeleteShader(vertexShader);
     496        gl::DeleteShader(fragmentShader);
     497        gl::DeleteProgram(yuvProgram);
     498    });
     499    // These are written so strlen might be compile-time.
     500    GLint vsLength = useTexture2D ? s_yuvVertexShaderTexture2D.length() : s_yuvVertexShaderTextureRectangle.length();
     501    GLint fsLength = useTexture2D ? s_yuvFragmentShaderTexture2D.length() : s_yuvFragmentShaderTextureRectangle.length();
     502    const char* vertexShaderSource = useTexture2D ? s_yuvVertexShaderTexture2D : s_yuvVertexShaderTextureRectangle;
     503    const char* fragmentShaderSource = useTexture2D ? s_yuvFragmentShaderTexture2D : s_yuvFragmentShaderTextureRectangle;
     504
     505    gl::ShaderSource(vertexShader, 1, &vertexShaderSource, &vsLength);
     506    gl::ShaderSource(fragmentShader, 1, &fragmentShaderSource, &fsLength);
     507    gl::CompileShader(vertexShader);
     508    gl::CompileShader(fragmentShader);
     509    gl::AttachShader(yuvProgram, vertexShader);
     510    gl::AttachShader(yuvProgram, fragmentShader);
     511    gl::LinkProgram(yuvProgram);
     512    // Link status is checked afterwards for theoretical parallel compilation benefit.
     513
     514    GLuint yuvVertexBuffer = 0;
     515    gl::GenBuffers(1, &yuvVertexBuffer);
     516    auto yuvVertexBufferCleanup = makeScopeExit([yuvVertexBuffer] {
     517        gl::DeleteBuffers(1, &yuvVertexBuffer);
     518    });
     519    float vertices[12] = { -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1 };
     520    gl::BindBuffer(GL_ARRAY_BUFFER, yuvVertexBuffer);
     521    gl::BufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     522
     523    GLuint framebuffer = 0;
     524    gl::GenFramebuffers(1, &framebuffer);
     525    auto framebufferCleanup = makeScopeExit([framebuffer] {
     526        gl::DeleteFramebuffers(1, &framebuffer);
     527    });
     528
     529    GLint status = 0;
     530    gl::GetProgramivRobustANGLE(yuvProgram, GL_LINK_STATUS, 1, nullptr, &status);
    486531    if (!status) {
    487         LOG(WebGL, "GraphicsContextGLCVANGLE::initializeUVContextObjects(%p) - Vertex shader failed to compile.", this);
    488         m_context->deleteShader(vertexShader);
    489         return false;
    490     }
    491 
    492     PlatformGLObject fragmentShader = m_context->createShader(GraphicsContextGL::FRAGMENT_SHADER);
    493     if (useTexture2D)
    494         m_context->shaderSource(fragmentShader, s_yuvFragmentShaderTexture2D);
    495     else
    496         m_context->shaderSource(fragmentShader, s_yuvFragmentShaderTextureRectangle);
    497 
    498     m_context->compileShaderDirect(fragmentShader);
    499 
    500     status = m_context->getShaderi(fragmentShader, GraphicsContextGL::COMPILE_STATUS);
    501     if (!status) {
    502         LOG(WebGL, "GraphicsContextGLCVANGLE::initializeUVContextObjects(%p) - Fragment shader failed to compile.", this);
    503         m_context->deleteShader(vertexShader);
    504         m_context->deleteShader(fragmentShader);
    505         return false;
    506     }
    507 
    508     m_yuvProgram = m_context->createProgram();
    509     m_context->attachShader(m_yuvProgram, vertexShader);
    510     m_context->attachShader(m_yuvProgram, fragmentShader);
    511     m_context->linkProgram(m_yuvProgram);
    512 
    513     status = m_context->getProgrami(m_yuvProgram, GraphicsContextGL::LINK_STATUS);
    514     if (!status) {
    515         LOG(WebGL, "GraphicsContextGLCVANGLE::initializeUVContextObjects(%p) - Program failed to link.", this);
    516         m_context->deleteShader(vertexShader);
    517         m_context->deleteShader(fragmentShader);
    518         m_context->deleteProgram(m_yuvProgram);
    519         m_yuvProgram = 0;
    520         return false;
    521     }
    522 
    523     m_yTextureUniformLocation = m_context->getUniformLocation(m_yuvProgram, "u_yTexture"_s);
    524     m_uvTextureUniformLocation = m_context->getUniformLocation(m_yuvProgram, "u_uvTexture"_s);
    525     m_colorMatrixUniformLocation = m_context->getUniformLocation(m_yuvProgram, "u_colorMatrix"_s);
    526     m_yuvFlipYUniformLocation = m_context->getUniformLocation(m_yuvProgram, "u_flipY"_s);
    527     m_yTextureSizeUniformLocation = m_context->getUniformLocation(m_yuvProgram, "u_yTextureSize"_s);
    528     m_uvTextureSizeUniformLocation = m_context->getUniformLocation(m_yuvProgram, "u_uvTextureSize"_s);
    529     m_yuvPositionAttributeLocation = m_context->getAttribLocationDirect(m_yuvProgram, "a_position"_s);
    530 
    531     m_context->detachShader(m_yuvProgram, vertexShader);
    532     m_context->detachShader(m_yuvProgram, fragmentShader);
    533     m_context->deleteShader(vertexShader);
    534     m_context->deleteShader(fragmentShader);
    535 
    536     m_yuvVertexBuffer = m_context->createBuffer();
    537     float vertices[12] = { -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1 };
    538 
    539     m_context->bindBuffer(GraphicsContextGL::ARRAY_BUFFER, m_yuvVertexBuffer);
    540     m_context->bufferData(GraphicsContextGL::ARRAY_BUFFER, GCGLSpan<const GCGLvoid>(vertices, sizeof(vertices)), GraphicsContextGL::STATIC_DRAW);
    541     m_context->enableVertexAttribArray(m_yuvPositionAttributeLocation);
    542     m_context->vertexAttribPointer(m_yuvPositionAttributeLocation, 2, GraphicsContextGL::FLOAT, false, 0, 0);
    543 
    544     return true;
    545 }
    546 
    547 void* GraphicsContextGLCVANGLE::attachIOSurfaceToTexture(GCGLenum target, GCGLenum internalFormat, GCGLsizei width, GCGLsizei height, GCGLenum type, IOSurfaceRef surface, GCGLuint plane)
    548 {
    549     auto display = m_context->platformDisplay();
    550     EGLint eglTextureTarget = 0;
    551 
    552     if (target == GraphicsContextGL::TEXTURE_RECTANGLE_ARB)
    553         eglTextureTarget = EGL_TEXTURE_RECTANGLE_ANGLE;
    554     else if (target == GraphicsContextGL::TEXTURE_2D)
    555         eglTextureTarget = EGL_TEXTURE_2D;
    556     else {
    557         LOG(WebGL, "Unknown texture target %d.", static_cast<int>(target));
    558         return nullptr;
    559     }
    560     if (eglTextureTarget != GraphicsContextGLOpenGL::EGLDrawingBufferTextureTarget()) {
    561         LOG(WebGL, "Mismatch in EGL texture target %d.", static_cast<int>(target));
    562         return nullptr;
    563     }
    564 
    565     const EGLint surfaceAttributes[] = {
    566         EGL_WIDTH, width,
    567         EGL_HEIGHT, height,
    568         EGL_IOSURFACE_PLANE_ANGLE, static_cast<EGLint>(plane),
    569         EGL_TEXTURE_TARGET, static_cast<EGLint>(eglTextureTarget),
    570         EGL_TEXTURE_INTERNAL_FORMAT_ANGLE, static_cast<EGLint>(internalFormat),
    571         EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,
    572         EGL_TEXTURE_TYPE_ANGLE, static_cast<EGLint>(type),
    573         // Only has an effect on the iOS Simulator.
    574         EGL_IOSURFACE_USAGE_HINT_ANGLE, EGL_IOSURFACE_READ_HINT_ANGLE,
    575         EGL_NONE, EGL_NONE
    576     };
    577     EGLSurface pbuffer = EGL_CreatePbufferFromClientBuffer(display, EGL_IOSURFACE_ANGLE, surface, m_context->platformConfig(), surfaceAttributes);
    578     if (!pbuffer)
    579         return nullptr;
    580     if (!EGL_BindTexImage(display, pbuffer, EGL_BACK_BUFFER)) {
    581         EGL_DestroySurface(display, pbuffer);
    582         return nullptr;
    583     }
    584     return pbuffer;
    585 }
    586 
    587 void GraphicsContextGLCVANGLE::detachIOSurfaceFromTexture(void* handle)
    588 {
    589     auto display = m_context->platformDisplay();
    590     EGL_ReleaseTexImage(display, handle, EGL_BACK_BUFFER);
    591     EGL_DestroySurface(display, handle);
    592 }
    593 
    594 bool GraphicsContextGLCVANGLE::copyPixelBufferToTexture(CVPixelBufferRef image, PlatformGLObject outputTexture, GCGLint level, GCGLenum internalFormat, GCGLenum format, GCGLenum type, FlipY flipY)
     532        GLint vsStatus = 0;
     533        gl::GetShaderivRobustANGLE(vertexShader, GL_COMPILE_STATUS, 1, nullptr, &vsStatus);
     534        GLint fsStatus = 0;
     535        gl::GetShaderivRobustANGLE(fragmentShader, GL_COMPILE_STATUS, 1, nullptr, &fsStatus);
     536        LOG(WebGL, "GraphicsContextGLCVANGLE(%p) - YUV program failed to link: %d, %d, %d.", this, status, vsStatus, fsStatus);
     537        return;
     538    }
     539    contextCleanup.release();
     540    yuvVertexBufferCleanup.release();
     541    framebufferCleanup.release();
     542    m_display = display;
     543    m_context = context;
     544    m_config = config;
     545    m_yuvVertexBuffer = yuvVertexBuffer;
     546    m_framebuffer = framebuffer;
     547    m_yTextureUniformLocation = gl::GetUniformLocation(yuvProgram, "u_yTexture");
     548    m_uvTextureUniformLocation = gl::GetUniformLocation(yuvProgram, "u_uvTexture");
     549    m_colorMatrixUniformLocation = gl::GetUniformLocation(yuvProgram, "u_colorMatrix");
     550    m_yuvFlipYUniformLocation = gl::GetUniformLocation(yuvProgram, "u_flipY");
     551    m_yTextureSizeUniformLocation = gl::GetUniformLocation(yuvProgram, "u_yTextureSize");
     552    m_uvTextureSizeUniformLocation = gl::GetUniformLocation(yuvProgram, "u_uvTextureSize");
     553    m_yuvPositionAttributeLocation = gl::GetAttribLocation(yuvProgram, "a_position");
     554    // Program is deleted by the cleanup while the program binary stays in use.
     555    gl::UseProgram(yuvProgram);
     556    gl::EnableVertexAttribArray(m_yuvPositionAttributeLocation);
     557    gl::VertexAttribPointer(m_yuvPositionAttributeLocation, 2, GL_FLOAT, false, 0, 0);
     558    gl::ClearColor(0, 0, 0, 0);
     559    gl::BindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
     560}
     561
     562bool GraphicsContextGLCVANGLE::copyPixelBufferToTexture(CVPixelBufferRef image, PlatformGLObject outputTexture, GLint level, GLenum internalFormat, GLenum format, GLenum type, FlipY flipY)
    595563{
    596564    // FIXME: This currently only supports '420v' and '420f' pixel formats. Investigate supporting more pixel formats.
     
    612580    auto newSurfaceSeed = IOSurfaceGetSeed(surface);
    613581    if (flipY == m_lastFlipY
    614         && surface == m_lastSurface 
     582        && surface == m_lastSurface
    615583        && newSurfaceSeed == m_lastSurfaceSeed
    616         && lastTextureSeed(outputTexture) == m_context->textureSeed(outputTexture)) {
     584        && lastTextureSeed(outputTexture) == m_owner.textureSeed(outputTexture)) {
    617585        // If the texture hasn't been modified since the last time we copied to it, and the
    618586        // image hasn't been modified since the last time it was copied, this is a no-op.
    619587        return true;
    620588    }
    621 
    622     if (!m_yuvProgram) {
    623         if (!initializeUVContextObjects()) {
    624             LOG(WebGL, "GraphicsContextGLCVANGLE::copyVideoTextureToPlatformTexture(%p) - Unable to initialize OpenGL context objects.", this);
    625             return false;
    626         }
    627     }
     589    if (!m_context || !GraphicsContextGLOpenGL::makeCurrent(m_display, m_context))
     590        return false;
     591
    628592    size_t width = CVPixelBufferGetWidth(image);
    629593    size_t height = CVPixelBufferGetHeight(image);
    630594
    631     m_context->bindFramebuffer(GraphicsContextGL::FRAMEBUFFER, m_framebuffer);
     595    gl::Viewport(0, 0, width, height);
    632596
    633597    // The outputTexture might contain uninitialized content on early-outs. Clear it in cases
    634598    // autoClearTextureOnError is not reset.
    635     auto autoClearTextureOnError = ScopedCleanup {
    636         [outputTexture, level, internalFormat, format, type, context = m_context.ptr()] {
    637             context->bindTexture(GraphicsContextGL::TEXTURE_2D, outputTexture);
    638             context->texImage2DDirect(GL_TEXTURE_2D, level, internalFormat, 0, 0, 0, format, type, nullptr);
    639             context->bindTexture(GraphicsContextGL::TEXTURE_2D, 0);
    640         }
    641     };
     599    auto autoClearTextureOnError = makeScopeExit([outputTexture, level, internalFormat, format, type] {
     600        gl::BindTexture(GL_TEXTURE_2D, outputTexture);
     601        gl::TexImage2D(GL_TEXTURE_2D, level, internalFormat, 0, 0, 0, format, type, nullptr);
     602        gl::BindTexture(GL_TEXTURE_2D, 0);
     603    });
    642604    // Allocate memory for the output texture.
    643     m_context->bindTexture(GraphicsContextGL::TEXTURE_2D, outputTexture);
    644     m_context->texParameteri(GraphicsContextGL::TEXTURE_2D, GraphicsContextGL::TEXTURE_MAG_FILTER, GraphicsContextGL::LINEAR);
    645     m_context->texParameteri(GraphicsContextGL::TEXTURE_2D, GraphicsContextGL::TEXTURE_MIN_FILTER, GraphicsContextGL::LINEAR);
    646     m_context->texParameteri(GraphicsContextGL::TEXTURE_2D, GraphicsContextGL::TEXTURE_WRAP_S, GraphicsContextGL::CLAMP_TO_EDGE);
    647     m_context->texParameteri(GraphicsContextGL::TEXTURE_2D, GraphicsContextGL::TEXTURE_WRAP_T, GraphicsContextGL::CLAMP_TO_EDGE);
    648     m_context->texImage2DDirect(GL_TEXTURE_2D, level, internalFormat, width, height, 0, format, type, nullptr);
    649 
    650     m_context->framebufferTexture2D(GraphicsContextGL::FRAMEBUFFER, GraphicsContextGL::COLOR_ATTACHMENT0, GraphicsContextGL::TEXTURE_2D, outputTexture, level);
    651     GCGLenum status = m_context->checkFramebufferStatus(GraphicsContextGL::FRAMEBUFFER);
    652     if (status != GraphicsContextGL::FRAMEBUFFER_COMPLETE) {
     605    gl::BindTexture(GL_TEXTURE_2D, outputTexture);
     606    gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     607    gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     608    gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     609    gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     610    gl::TexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, 0, format, type, nullptr);
     611
     612    gl::FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputTexture, level);
     613    GLenum status = gl::CheckFramebufferStatus(GL_FRAMEBUFFER);
     614    if (status != GL_FRAMEBUFFER_COMPLETE) {
    653615        LOG(WebGL, "GraphicsContextGLCVANGLE::copyVideoTextureToPlatformTexture(%p) - Unable to create framebuffer for outputTexture.", this);
    654616        return false;
    655617    }
    656     m_context->bindTexture(GraphicsContextGL::TEXTURE_2D, 0);
    657 
    658     m_context->useProgram(m_yuvProgram);
    659     m_context->viewport(0, 0, width, height);
     618    gl::BindTexture(GL_TEXTURE_2D, 0);
    660619
    661620    // Bind and set up the textures for the video source.
     
    665624    auto uvPlaneHeight = IOSurfaceGetHeightOfPlane(surface, 1);
    666625
    667     GCGLenum videoTextureTarget = GraphicsContextGLOpenGL::drawingBufferTextureTarget();
    668 
    669     auto uvTexture = m_context->createTexture();
    670     m_context->activeTexture(GraphicsContextGL::TEXTURE1);
    671     m_context->bindTexture(videoTextureTarget, uvTexture);
    672     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_MAG_FILTER, GraphicsContextGL::LINEAR);
    673     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_MIN_FILTER, GraphicsContextGL::LINEAR);
    674     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_WRAP_S, GraphicsContextGL::CLAMP_TO_EDGE);
    675     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_WRAP_T, GraphicsContextGL::CLAMP_TO_EDGE);
    676     auto uvHandle = attachIOSurfaceToTexture(videoTextureTarget, GraphicsContextGL::RG, uvPlaneWidth, uvPlaneHeight, GraphicsContextGL::UNSIGNED_BYTE, surface, 1);
    677     if (!uvHandle) {
    678         m_context->deleteTexture(uvTexture);
     626    GLenum videoTextureTarget = GraphicsContextGLOpenGL::drawingBufferTextureTarget();
     627
     628    GLuint uvTexture = 0;
     629    gl::GenTextures(1, &uvTexture);
     630    auto uvTextureCleanup = makeScopeExit([uvTexture] {
     631        gl::DeleteTextures(1, &uvTexture);
     632    });
     633    gl::ActiveTexture(GL_TEXTURE1);
     634    gl::BindTexture(videoTextureTarget, uvTexture);
     635    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     636    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     637    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     638    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     639    auto uvHandle = WebCore::createPbufferAndAttachIOSurface(m_display, m_config, videoTextureTarget, EGL_IOSURFACE_READ_HINT_ANGLE, GL_RG, uvPlaneWidth, uvPlaneHeight, GL_UNSIGNED_BYTE, surface, 1);
     640    if (!uvHandle)
    679641        return false;
    680     }
    681 
    682     auto yTexture = m_context->createTexture();
    683     m_context->activeTexture(GraphicsContextGL::TEXTURE0);
    684     m_context->bindTexture(videoTextureTarget, yTexture);
    685     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_MAG_FILTER, GraphicsContextGL::LINEAR);
    686     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_MIN_FILTER, GraphicsContextGL::LINEAR);
    687     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_WRAP_S, GraphicsContextGL::CLAMP_TO_EDGE);
    688     m_context->texParameteri(videoTextureTarget, GraphicsContextGL::TEXTURE_WRAP_T, GraphicsContextGL::CLAMP_TO_EDGE);
    689     auto yHandle = attachIOSurfaceToTexture(videoTextureTarget, GraphicsContextGL::RED, yPlaneWidth, yPlaneHeight, GraphicsContextGL::UNSIGNED_BYTE, surface, 0);
    690     if (!yHandle) {
    691         m_context->deleteTexture(yTexture);
    692         m_context->deleteTexture(uvTexture);
     642    auto uvHandleCleanup = makeScopeExit([display = m_display, uvHandle] {
     643        WebCore::destroyPbufferAndDetachIOSurface(display, uvHandle);
     644    });
     645
     646    GLuint yTexture = 0;
     647    gl::GenTextures(1, &yTexture);
     648    auto yTextureCleanup = makeScopeExit([yTexture] {
     649        gl::DeleteTextures(1, &yTexture);
     650    });
     651    gl::ActiveTexture(GL_TEXTURE0);
     652    gl::BindTexture(videoTextureTarget, yTexture);
     653    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     654    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     655    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     656    gl::TexParameteri(videoTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     657    auto yHandle = WebCore::createPbufferAndAttachIOSurface(m_display, m_config, videoTextureTarget, EGL_IOSURFACE_READ_HINT_ANGLE, GL_RED, yPlaneWidth, yPlaneHeight, GL_UNSIGNED_BYTE, surface, 0);
     658    if (!yHandle)
    693659        return false;
    694     }
     660    auto yHandleCleanup = makeScopeExit([display = m_display, yHandle] {
     661        destroyPbufferAndDetachIOSurface(display, yHandle);
     662    });
    695663
    696664    // Configure the drawing parameters.
    697     m_context->uniform1i(m_yTextureUniformLocation, 0);
    698     m_context->uniform1i(m_uvTextureUniformLocation, 1);
    699     m_context->uniform1i(m_yuvFlipYUniformLocation, flipY == FlipY::Yes ? 1 : 0);
    700     m_context->uniform2f(m_yTextureSizeUniformLocation, yPlaneWidth, yPlaneHeight);
    701     m_context->uniform2f(m_uvTextureSizeUniformLocation, uvPlaneWidth, uvPlaneHeight);
     665    gl::Uniform1i(m_yTextureUniformLocation, 0);
     666    gl::Uniform1i(m_uvTextureUniformLocation, 1);
     667    gl::Uniform1i(m_yuvFlipYUniformLocation, flipY == FlipY::Yes ? 1 : 0);
     668    gl::Uniform2f(m_yTextureSizeUniformLocation, yPlaneWidth, yPlaneHeight);
     669    gl::Uniform2f(m_uvTextureSizeUniformLocation, uvPlaneWidth, uvPlaneHeight);
    702670
    703671    auto range = pixelRangeFromPixelFormat(pixelFormat);
    704672    auto transferFunction = transferFunctionFromString(dynamic_cf_cast<CFStringRef>(CVBufferGetAttachment(image, kCVImageBufferYCbCrMatrixKey, nil)));
    705673    auto colorMatrix = YCbCrToRGBMatrixForRangeAndTransferFunction(range, transferFunction);
    706     m_context->uniformMatrix4fv(m_colorMatrixUniformLocation, GL_FALSE, colorMatrix);
     674    gl::UniformMatrix4fv(m_colorMatrixUniformLocation, 1, GL_FALSE, colorMatrix);
    707675
    708676    // Do the actual drawing.
    709     m_context->drawArrays(GraphicsContextGL::TRIANGLES, 0, 6);
    710 
    711     // Clean-up.
    712     m_context->deleteTexture(yTexture);
    713     m_context->deleteTexture(uvTexture);
    714     detachIOSurfaceFromTexture(yHandle);
    715     detachIOSurfaceFromTexture(uvHandle);
     677    gl::DrawArrays(GL_TRIANGLES, 0, 6);
    716678
    717679    m_lastSurface = surface;
    718680    m_lastSurfaceSeed = newSurfaceSeed;
    719     m_lastTextureSeed.set(outputTexture, m_context->textureSeed(outputTexture));
     681    m_lastTextureSeed.set(outputTexture, m_owner.textureSeed(outputTexture));
    720682    m_lastFlipY = flipY;
    721     autoClearTextureOnError.reset();
     683    autoClearTextureOnError.release();
    722684    return true;
    723685}
  • trunk/Source/WebCore/platform/graphics/cv/GraphicsContextGLCVANGLE.h

    r270477 r283362  
    2828#if ENABLE(WEBGL) && ENABLE(VIDEO) && USE(AVFOUNDATION)
    2929
    30 #import "GraphicsContextGLCV.h"
    31 #import "GraphicsContextGLOpenGL.h"
     30#include "GraphicsContextGLCV.h"
    3231
    33 #import <wtf/UnsafePointer.h>
     32#include <memory>
     33#include <wtf/UnsafePointer.h>
    3434
    3535namespace WebCore {
     36class GraphicsContextGLOpenGL;
    3637
    3738// GraphicsContextGLCV implementation for ANGLE flavour of GraphicsContextGLOpenGL.
     39// This class is part of the internal implementation of GraphicsContextGLOpenGL for ANGLE Cocoa.
    3840class GraphicsContextGLCVANGLE final : public GraphicsContextGLCV {
    3941    WTF_MAKE_FAST_ALLOCATED;
    4042public:
    41     GraphicsContextGLCVANGLE(GraphicsContextGLOpenGL&);
     43    static std::unique_ptr<GraphicsContextGLCVANGLE> create(GraphicsContextGLOpenGL&);
     44
    4245    ~GraphicsContextGLCVANGLE() final;
    4346
     
    4548
    4649private:
    47     bool initializeUVContextObjects();
     50    GraphicsContextGLCVANGLE(GraphicsContextGLOpenGL&);
    4851
    4952    unsigned lastTextureSeed(GCGLuint texture)
     
    5255    }
    5356
    54     // Returns a handle which, if non-null, must be released via the
    55     // detach call below.
    56     void* attachIOSurfaceToTexture(GCGLenum target, GCGLenum internalFormat, GCGLsizei width, GCGLsizei height, GCGLenum type, IOSurfaceRef, GCGLuint plane);
    57     void detachIOSurfaceFromTexture(void* handle);
    58 
    59     Ref<GraphicsContextGLOpenGL> m_context;
     57    GraphicsContextGLOpenGL& m_owner;
     58    PlatformGraphicsContextGLDisplay m_display { nullptr };
     59    PlatformGraphicsContextGL m_context { nullptr };
     60    PlatformGraphicsContextGLConfig m_config { nullptr };
    6061
    6162    PlatformGLObject m_framebuffer { 0 };
    62     PlatformGLObject m_yuvProgram { 0 };
    6363    PlatformGLObject m_yuvVertexBuffer { 0 };
    6464    GCGLint m_yTextureUniformLocation { -1 };
  • trunk/Source/WebCore/platform/graphics/opengl/GraphicsContextGLOpenGL.h

    r283308 r283362  
    7070namespace WebCore {
    7171class GraphicsContextGLIOSurfaceSwapChain;
     72class GraphicsContextGLCVANGLE;
    7273}
    7374#endif // PLATFORM(COCOA)
     
    105106
    106107#if PLATFORM(COCOA)
    107     static Ref<GraphicsContextGLOpenGL> createShared(GraphicsContextGLOpenGL& sharedContext);
    108108    static Ref<GraphicsContextGLOpenGL> createForGPUProcess(const GraphicsContextGLAttributes&, GraphicsContextGLIOSurfaceSwapChain*);
    109109
     
    543543private:
    544544#if PLATFORM(COCOA)
    545     GraphicsContextGLOpenGL(GraphicsContextGLAttributes, HostWindow*, GraphicsContextGLOpenGL* sharedContext = nullptr, GraphicsContextGLIOSurfaceSwapChain* = nullptr);
     545    GraphicsContextGLOpenGL(GraphicsContextGLAttributes, HostWindow*, GraphicsContextGLIOSurfaceSwapChain* = nullptr);
    546546#else
    547     GraphicsContextGLOpenGL(GraphicsContextGLAttributes, HostWindow*, GraphicsContextGLOpenGL* sharedContext = nullptr);
     547    GraphicsContextGLOpenGL(GraphicsContextGLAttributes, HostWindow*);
    548548#endif
    549549
     
    551551    // Called once by all the public entry points of ExtensionsGL that eventually call OpenGL.
    552552    bool makeContextCurrent() WARN_UNUSED_RETURN;
    553     void clearCurrentContext();
    554553
    555554    // Take into account the user's requested context creation attributes,
     
    578577    bool allocateAndBindDisplayBufferBacking();
    579578    bool bindDisplayBufferBacking(std::unique_ptr<IOSurface> backing, void* pbuffer);
     579    static bool makeCurrent(PlatformGraphicsContextGLDisplay, PlatformGraphicsContextGL);
     580    friend class GraphicsContextGLCVANGLE;
    580581#endif
    581582#if USE(ANGLE)
     
    794795#endif
    795796#if ENABLE(VIDEO) && USE(AVFOUNDATION)
    796     std::unique_ptr<GraphicsContextGLCV> m_cv;
     797    std::unique_ptr<GraphicsContextGLCVANGLE> m_cv;
    797798#endif
    798799#if USE(ANGLE)
  • trunk/Source/WebCore/platform/graphics/texmap/GraphicsContextGLTextureMapper.cpp

    r276528 r283362  
    105105
    106106#if USE(ANGLE)
    107 GraphicsContextGLOpenGL::GraphicsContextGLOpenGL(GraphicsContextGLAttributes attributes, HostWindow*, GraphicsContextGLOpenGL* sharedContext)
    108     : GraphicsContextGL(attributes, sharedContext)
    109 {
    110     ASSERT_UNUSED(sharedContext, !sharedContext);
     107GraphicsContextGLOpenGL::GraphicsContextGLOpenGL(GraphicsContextGLAttributes attributes, HostWindow*)
     108    : GraphicsContextGL(attributes)
     109{
    111110#if ENABLE(WEBGL2)
    112111    m_isForWebGL2 = attributes.webGLVersion == GraphicsContextGLWebGLVersion::WebGL2;
     
    175174}
    176175#else
    177 GraphicsContextGLOpenGL::GraphicsContextGLOpenGL(GraphicsContextGLAttributes attributes, HostWindow*, GraphicsContextGLOpenGL* sharedContext)
    178     : GraphicsContextGL(attributes, sharedContext)
    179 {
    180     ASSERT_UNUSED(sharedContext, !sharedContext);
     176GraphicsContextGLOpenGL::GraphicsContextGLOpenGL(GraphicsContextGLAttributes attributes, HostWindow*)
     177    : GraphicsContextGL(attributes)
     178{
    181179#if USE(NICOSIA)
    182180    m_nicosiaLayer = makeUnique<Nicosia::GCGLLayer>(*this);
Note: See TracChangeset for help on using the changeset viewer.