• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(3D_CANVAS)
30
31#import "Canvas3DLayer.h"
32
33#import "GraphicsLayer.h"
34#import <QuartzCore/QuartzCore.h>
35#import <OpenGL/OpenGL.h>
36#import <wtf/FastMalloc.h>
37#import <wtf/RetainPtr.h>
38#import <wtf/UnusedParam.h>
39
40using namespace WebCore;
41
42@implementation Canvas3DLayer
43
44-(id)initWithContext:(CGLContextObj)context texture:(GLuint)texture
45{
46    m_contextObj = context;
47    m_texture = texture;
48    self = [super init];
49    return self;
50}
51
52-(CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
53{
54    // FIXME: The mask param tells you which display (on a multi-display system)
55    // is to be used. But since we are now getting the pixel format from the
56    // Canvas CGL context, we don't use it. This seems to do the right thing on
57    // one multi-display system. But there may be cases where this is not the case.
58    // If needed we will have to set the display mask in the Canvas CGLContext and
59    // make sure it matches.
60    UNUSED_PARAM(mask);
61    return CGLRetainPixelFormat(CGLGetPixelFormat(m_contextObj));
62}
63
64-(CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
65{
66    CGLContextObj contextObj;
67    CGLCreateContext(pixelFormat, m_contextObj, &contextObj);
68    return contextObj;
69}
70
71-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
72{
73    CGLSetCurrentContext(m_contextObj);
74    glFinish();
75    CGLSetCurrentContext(glContext);
76
77    CGRect frame = [self frame];
78
79    // draw the FBO into the layer
80    glViewport(0, 0, frame.size.width, frame.size.height);
81    glMatrixMode(GL_PROJECTION);
82    glLoadIdentity();
83    glOrtho(-1, 1, -1, 1, -1, 1);
84    glMatrixMode(GL_MODELVIEW);
85    glLoadIdentity();
86
87    glEnable(GL_TEXTURE_2D);
88    glBindTexture(GL_TEXTURE_2D, m_texture);
89
90    glBegin(GL_TRIANGLE_FAN);
91        glTexCoord2f(0, 0);
92        glVertex2f(-1, -1);
93        glTexCoord2f(1, 0);
94        glVertex2f(1, -1);
95        glTexCoord2f(1, 1);
96        glVertex2f(1, 1);
97        glTexCoord2f(0, 1);
98        glVertex2f(-1, 1);
99    glEnd();
100
101    glBindTexture(GL_TEXTURE_2D, 0);
102    glDisable(GL_TEXTURE_2D);
103
104    // Call super to finalize the drawing. By default all it does is call glFlush().
105    [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
106}
107
108static void freeData(void *, const void *data, size_t /* size */)
109{
110    fastFree(const_cast<void *>(data));
111}
112
113-(CGImageRef)copyImageSnapshotWithColorSpace:(CGColorSpaceRef)colorSpace
114{
115    CGLSetCurrentContext(m_contextObj);
116
117    RetainPtr<CGColorSpaceRef> imageColorSpace = colorSpace;
118    if (!imageColorSpace)
119        imageColorSpace.adoptCF(CGColorSpaceCreateDeviceRGB());
120
121    CGRect layerBounds = CGRectIntegral([self bounds]);
122
123    size_t width = layerBounds.size.width;
124    size_t height = layerBounds.size.height;
125
126    size_t rowBytes = (width * 4 + 15) & ~15;
127    size_t dataSize = rowBytes * height;
128    void* data = fastMalloc(dataSize);
129    if (!data)
130        return 0;
131
132    glPixelStorei(GL_PACK_ROW_LENGTH, rowBytes / 4);
133    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
134
135    CGDataProviderRef provider = CGDataProviderCreateWithData(0, data, dataSize, freeData);
136    CGImageRef image = CGImageCreate(width, height, 8, 32, rowBytes, imageColorSpace.get(),
137                                                 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
138                                                 provider, 0, true,
139                                                 kCGRenderingIntentDefault);
140    CGDataProviderRelease(provider);
141    return image;
142}
143
144- (void)display
145{
146    [super display];
147    if (m_layerOwner)
148        m_layerOwner->didDisplay(self);
149}
150
151@end
152
153@implementation Canvas3DLayer(WebLayerAdditions)
154
155-(void)setLayerOwner:(GraphicsLayer*)aLayer
156{
157    m_layerOwner = aLayer;
158}
159
160-(GraphicsLayer*)layerOwner
161{
162    return m_layerOwner;
163}
164
165@end
166
167#endif // ENABLE(3D_CANVAS)
168#endif // USE(ACCELERATED_COMPOSITING)
169