2010-05-24 Jer Noble <jer.noble@apple.com>
[WebKit-https.git] / WebCore / platform / graphics / win / QTPixelBuffer.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 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 #include "config.h"
26
27 #include "QTPixelBuffer.h"
28
29 #include <CFString.h>
30 #include <CGColorSpace.h>
31 #include <CGImage.h>
32 #include <CVPixelBuffer.h>
33 #include <QuickDraw.h>
34 #include <memory.h>
35
36 QTPixelBuffer::QTPixelBuffer() 
37     : m_pixelBuffer(0) 
38 {
39 }
40
41 QTPixelBuffer::QTPixelBuffer(const QTPixelBuffer& p) 
42     : m_pixelBuffer(p.m_pixelBuffer) 
43 {
44     CVPixelBufferRetain(m_pixelBuffer);
45 }
46
47 QTPixelBuffer::QTPixelBuffer(CVPixelBufferRef ref) 
48     : m_pixelBuffer(ref)
49 {
50     CVPixelBufferRetain(m_pixelBuffer);
51 }
52
53 QTPixelBuffer::~QTPixelBuffer() 
54 {
55     clear();
56 }
57
58 QTPixelBuffer& QTPixelBuffer::operator=(const QTPixelBuffer& p) 
59 {
60     set(p.m_pixelBuffer);
61     return *this;
62 }
63
64 void QTPixelBuffer::set(CVPixelBufferRef ref)
65 {
66     CVPixelBufferRetain(ref);
67     CVPixelBufferRelease(m_pixelBuffer);
68     m_pixelBuffer = ref;
69 }
70
71 CVPixelBufferRef QTPixelBuffer::pixelBufferRef()
72 {
73     return m_pixelBuffer;
74 }
75
76 void QTPixelBuffer::adopt(CVPixelBufferRef ref)
77 {
78     if (ref == m_pixelBuffer)
79         return;
80     CVPixelBufferRelease(m_pixelBuffer);
81     m_pixelBuffer = ref;
82 }
83
84 void QTPixelBuffer::clear()
85 {
86     CVPixelBufferRelease(m_pixelBuffer);
87     m_pixelBuffer = 0;
88 }
89
90 CVReturn QTPixelBuffer::lockBaseAddress()
91 {
92     return CVPixelBufferLockBaseAddress(m_pixelBuffer, 0);
93 }
94
95 CVReturn QTPixelBuffer::unlockBaseAddress()
96 {
97     return CVPixelBufferUnlockBaseAddress(m_pixelBuffer, 0);
98 }
99
100 void* QTPixelBuffer::baseAddress()
101 {
102     return CVPixelBufferGetBaseAddress(m_pixelBuffer);
103 }
104
105 size_t QTPixelBuffer::width() const
106 {
107     return CVPixelBufferGetWidth(m_pixelBuffer);
108 }
109
110 size_t QTPixelBuffer::height() const
111 {
112     return CVPixelBufferGetHeight(m_pixelBuffer);
113 }
114
115 unsigned long QTPixelBuffer::pixelFormatType() const
116 {
117     return CVPixelBufferGetPixelFormatType(m_pixelBuffer);
118 }
119
120 bool QTPixelBuffer::pixelFormatIs32ARGB() const
121 {
122     return CVPixelBufferGetPixelFormatType(m_pixelBuffer) == k32ARGBPixelFormat;
123 }
124
125 bool QTPixelBuffer::pixelFormatIs32BGRA() const
126 {
127     return CVPixelBufferGetPixelFormatType(m_pixelBuffer) == k32BGRAPixelFormat;
128 }
129
130 size_t QTPixelBuffer::bytesPerRow() const
131 {
132     return CVPixelBufferGetBytesPerRow(m_pixelBuffer);
133 }
134
135 size_t QTPixelBuffer::dataSize() const
136 {
137     return CVPixelBufferGetDataSize(m_pixelBuffer);
138 }
139
140 bool QTPixelBuffer::isPlanar() const
141 {
142     return CVPixelBufferIsPlanar(m_pixelBuffer);
143 }
144
145 size_t QTPixelBuffer::planeCount() const
146 {
147     return CVPixelBufferGetPlaneCount(m_pixelBuffer);
148 }
149
150 size_t QTPixelBuffer::widthOfPlane(size_t plane) const
151 {
152     return CVPixelBufferGetWidthOfPlane(m_pixelBuffer, plane);
153 }
154
155 size_t QTPixelBuffer::heightOfPlane(size_t plane) const
156 {
157     return CVPixelBufferGetHeightOfPlane(m_pixelBuffer, plane);
158 }
159
160 void* QTPixelBuffer::baseAddressOfPlane(size_t plane) const
161 {
162     return CVPixelBufferGetBaseAddressOfPlane(m_pixelBuffer, plane);
163 }
164
165 size_t QTPixelBuffer::bytesPerRowOfPlane(size_t plane) const
166 {
167     return CVPixelBufferGetBytesPerRowOfPlane(m_pixelBuffer, plane);
168 }
169
170 void QTPixelBuffer::getExtendedPixels(size_t* left, size_t* right, size_t* top, size_t* bottom) const
171 {
172     return CVPixelBufferGetExtendedPixels(m_pixelBuffer, left, right, top, bottom);
173 }
174
175 CFDictionaryRef QTPixelBuffer::attachments() const
176 {
177     return CVBufferGetAttachments(m_pixelBuffer, kCVAttachmentMode_ShouldPropagate);
178 }
179
180 void QTPixelBuffer::retainCallback(void* refcon)
181 {
182     CVPixelBufferRetain(static_cast<CVPixelBufferRef>(refcon));
183 }
184
185 void QTPixelBuffer::releaseCallback(void* refcon)
186 {
187     CVPixelBufferRelease(static_cast<CVPixelBufferRef>(refcon));
188 }
189
190 void QTPixelBuffer::imageQueueReleaseCallback(unsigned int type, uint64_t id, void* refcon)
191 {
192     CVPixelBufferRelease(static_cast<CVPixelBufferRef>(refcon));
193 }
194
195 void QTPixelBuffer::dataProviderReleaseBytePointerCallback(void* refcon, const void* pointer)
196 {
197     CVPixelBufferUnlockBaseAddress(static_cast<CVPixelBufferRef>(refcon), 0);
198 }
199
200 const void* QTPixelBuffer::dataProviderGetBytePointerCallback(void* refcon)
201 {
202     CVPixelBufferLockBaseAddress(static_cast<CVPixelBufferRef>(refcon), 0);
203     return CVPixelBufferGetBaseAddress(static_cast<CVPixelBufferRef>(refcon));
204 }
205
206 size_t QTPixelBuffer::dataProviderGetBytesAtPositionCallback(void* refcon, void* buffer, size_t position, size_t count)
207 {
208     char* data = (char*)CVPixelBufferGetBaseAddress(static_cast<CVPixelBufferRef>(refcon));
209     size_t size = CVPixelBufferGetDataSize(static_cast<CVPixelBufferRef>(refcon));
210     if (size - position < count)
211         count = size - position;
212
213     memcpy(buffer, data+position, count);
214     return count;
215 }
216
217 void QTPixelBuffer::dataProviderReleaseInfoCallback(void* refcon)
218 {
219     CVPixelBufferRelease(static_cast<CVPixelBufferRef>(refcon));
220 }