initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / LayerTextureSubImage.cpp
1 /*
2  * Copyright (C) 2011 Google 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if USE(ACCELERATED_COMPOSITING)
29
30 #include "LayerTextureSubImage.h"
31
32 #include "Extensions3DChromium.h"
33 #include "GraphicsContext3D.h"
34 #include "TraceEvent.h"
35
36 namespace WebCore {
37
38 LayerTextureSubImage::LayerTextureSubImage(bool useMapTexSubImage)
39     : m_useMapTexSubImage(useMapTexSubImage)
40 {
41 }
42
43 LayerTextureSubImage::~LayerTextureSubImage()
44 {
45 }
46
47 void LayerTextureSubImage::setSubImageSize(const IntSize& subImageSize)
48 {
49     if (subImageSize == m_subImageSize)
50         return;
51
52     m_subImageSize = subImageSize;
53     m_subImage.clear();
54 }
55
56 void LayerTextureSubImage::upload(const uint8_t* image, const IntRect& imageRect,
57                                   const IntRect& sourceRect, const IntRect& destRect,
58                                   GC3Denum format, GraphicsContext3D* context)
59 {
60     if (m_useMapTexSubImage)
61         uploadWithMapTexSubImage(image, imageRect, sourceRect, destRect, format, context);
62     else
63         uploadWithTexSubImage(image, imageRect, sourceRect, destRect, format, context);
64 }
65
66 void LayerTextureSubImage::uploadWithTexSubImage(const uint8_t* image, const IntRect& imageRect,
67                                                  const IntRect& sourceRect, const IntRect& destRect,
68                                                  GC3Denum format, GraphicsContext3D* context)
69 {
70     TRACE_EVENT("LayerTextureSubImage::uploadWithTexSubImage", this, 0);
71     if (!m_subImage)
72         m_subImage = adoptArrayPtr(new uint8_t[m_subImageSize.width() * m_subImageSize.height() * 4]);
73
74     // Offset from image-rect to source-rect.
75     IntPoint offset(sourceRect.x() - imageRect.x(), sourceRect.y() - imageRect.y());
76
77     const uint8_t* pixelSource;
78     if (imageRect.width() == sourceRect.width() && !offset.x())
79         pixelSource = &image[4 * offset.y() * imageRect.width()];
80     else {
81         // Strides not equal, so do a row-by-row memcpy from the
82         // paint results into a temp buffer for uploading.
83         for (int row = 0; row < destRect.height(); ++row)
84             memcpy(&m_subImage[destRect.width() * 4 * row],
85                    &image[4 * (offset.x() + (offset.y() + row) * imageRect.width())],
86                    destRect.width() * 4);
87
88         pixelSource = &m_subImage[0];
89     }
90     context->texSubImage2D(GraphicsContext3D::TEXTURE_2D, 0, destRect.x(), destRect.y(), destRect.width(), destRect.height(), format, GraphicsContext3D::UNSIGNED_BYTE, pixelSource);
91 }
92
93 void LayerTextureSubImage::uploadWithMapTexSubImage(const uint8_t* image, const IntRect& imageRect,
94                                                     const IntRect& sourceRect, const IntRect& destRect,
95                                                     GC3Denum format, GraphicsContext3D* context)
96 {
97     TRACE_EVENT("LayerTextureSubImage::uploadWithMapTexSubImage", this, 0);
98     // Offset from image-rect to source-rect.
99     IntPoint offset(sourceRect.x() - imageRect.x(), sourceRect.y() - imageRect.y());
100
101     // Upload tile data via a mapped transfer buffer
102     Extensions3DChromium* extensions = static_cast<Extensions3DChromium*>(context->getExtensions());
103     uint8_t* pixelDest = static_cast<uint8_t*>(extensions->mapTexSubImage2DCHROMIUM(GraphicsContext3D::TEXTURE_2D, 0, destRect.x(), destRect.y(), destRect.width(), destRect.height(), format, GraphicsContext3D::UNSIGNED_BYTE, Extensions3DChromium::WRITE_ONLY));
104
105     if (!pixelDest) {
106         uploadWithTexSubImage(image, imageRect, sourceRect, destRect, format, context);
107         return;
108     }
109
110     if (imageRect.width() == sourceRect.width() && !offset.x())
111         memcpy(pixelDest, &image[4 * offset.y() * imageRect.width()], imageRect.width() * destRect.height() * 4);
112     else {
113         // Strides not equal, so do a row-by-row memcpy from the
114         // paint results into the pixelDest
115         for (int row = 0; row < destRect.height(); ++row)
116             memcpy(&pixelDest[destRect.width() * 4 * row],
117                    &image[4 * (offset.x() + (offset.y() + row) * imageRect.width())],
118                    destRect.width() * 4);
119     }
120     extensions->unmapTexSubImage2DCHROMIUM(pixelDest);
121 }
122
123 } // namespace WebCore
124
125 #endif // USE(ACCELERATED_COMPOSITING)