1 /* 2 * Copyright (C) 2009 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 28 #if USE(ACCELERATED_COMPOSITING) 29 #if ENABLE(WEBGL) 30 31 #import "WebGLLayer.h" 32 33 #import "GraphicsContext3D.h" 34 #import "GraphicsLayer.h" 35 #import <OpenGL/OpenGL.h> 36 #import <wtf/FastMalloc.h> 37 #import <wtf/RetainPtr.h> 38 #import <wtf/UnusedParam.h> 39 40 using namespace WebCore; 41 42 @implementation WebGLLayer 43 44 -(id)initWithGraphicsContext3D:(GraphicsContext3D*)context 45 { 46 m_context = context; 47 self = [super init]; 48 return self; 49 } 50 51 -(CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask 52 { 53 // FIXME: The mask param tells you which display (on a multi-display system) 54 // is to be used. But since we are now getting the pixel format from the 55 // Canvas CGL context, we don't use it. This seems to do the right thing on 56 // one multi-display system. But there may be cases where this is not the case. 57 // If needed we will have to set the display mask in the Canvas CGLContext and 58 // make sure it matches. 59 UNUSED_PARAM(mask); 60 return CGLRetainPixelFormat(CGLGetPixelFormat(m_context->platformGraphicsContext3D())); 61 } 62 63 -(CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat 64 { 65 CGLContextObj contextObj; 66 CGLCreateContext(pixelFormat, m_context->platformGraphicsContext3D(), &contextObj); 67 return contextObj; 68 } 69 70 -(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp 71 { 72 m_context->prepareTexture(); 73 74 CGLSetCurrentContext(glContext); 75 76 CGRect frame = [self frame]; 77 78 // draw the FBO into the layer 79 glViewport(0, 0, frame.size.width, frame.size.height); 80 glMatrixMode(GL_PROJECTION); 81 glLoadIdentity(); 82 glOrtho(-1, 1, -1, 1, -1, 1); 83 glMatrixMode(GL_MODELVIEW); 84 glLoadIdentity(); 85 86 glEnable(GL_TEXTURE_2D); 87 glBindTexture(GL_TEXTURE_2D, m_context->platformTexture()); 88 89 glBegin(GL_TRIANGLE_FAN); 90 glTexCoord2f(0, 0); 91 glVertex2f(-1, -1); 92 glTexCoord2f(1, 0); 93 glVertex2f(1, -1); 94 glTexCoord2f(1, 1); 95 glVertex2f(1, 1); 96 glTexCoord2f(0, 1); 97 glVertex2f(-1, 1); 98 glEnd(); 99 100 glBindTexture(GL_TEXTURE_2D, 0); 101 glDisable(GL_TEXTURE_2D); 102 103 // Call super to finalize the drawing. By default all it does is call glFlush(). 104 [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp]; 105 } 106 107 static void freeData(void *, const void *data, size_t /* size */) 108 { 109 fastFree(const_cast<void *>(data)); 110 } 111 112 -(CGImageRef)copyImageSnapshotWithColorSpace:(CGColorSpaceRef)colorSpace 113 { 114 CGLSetCurrentContext(m_context->platformGraphicsContext3D()); 115 116 RetainPtr<CGColorSpaceRef> imageColorSpace = colorSpace; 117 if (!imageColorSpace) 118 imageColorSpace.adoptCF(CGColorSpaceCreateDeviceRGB()); 119 120 CGRect layerBounds = CGRectIntegral([self bounds]); 121 122 size_t width = layerBounds.size.width; 123 size_t height = layerBounds.size.height; 124 125 size_t rowBytes = (width * 4 + 15) & ~15; 126 size_t dataSize = rowBytes * height; 127 void* data = fastMalloc(dataSize); 128 if (!data) 129 return 0; 130 131 glPixelStorei(GL_PACK_ROW_LENGTH, rowBytes / 4); 132 glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data); 133 134 CGDataProviderRef provider = CGDataProviderCreateWithData(0, data, dataSize, freeData); 135 CGImageRef image = CGImageCreate(width, height, 8, 32, rowBytes, imageColorSpace.get(), 136 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host, 137 provider, 0, true, 138 kCGRenderingIntentDefault); 139 CGDataProviderRelease(provider); 140 return image; 141 } 142 143 - (void)display 144 { 145 [super display]; 146 if (m_layerOwner) 147 m_layerOwner->layerDidDisplay(self); 148 } 149 150 @end 151 152 @implementation WebGLLayer(WebGLLayerAdditions) 153 154 -(void)setLayerOwner:(GraphicsLayer*)aLayer 155 { 156 m_layerOwner = aLayer; 157 } 158 159 -(GraphicsLayer*)layerOwner 160 { 161 return m_layerOwner; 162 } 163 164 @end 165 166 #endif // ENABLE(WEBGL) 167 #endif // USE(ACCELERATED_COMPOSITING) 168