2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if USE(ACCELERATED_COMPOSITING)
30 #include "LayerTextureSubImage.h"
32 #include "Extensions3DChromium.h"
33 #include "GraphicsContext3D.h"
34 #include "TraceEvent.h"
38 LayerTextureSubImage::LayerTextureSubImage(bool useMapTexSubImage)
39 : m_useMapTexSubImage(useMapTexSubImage)
43 LayerTextureSubImage::~LayerTextureSubImage()
47 void LayerTextureSubImage::setSubImageSize(const IntSize& subImageSize)
49 if (subImageSize == m_subImageSize)
52 m_subImageSize = subImageSize;
56 void LayerTextureSubImage::upload(const uint8_t* image, const IntRect& imageRect,
57 const IntRect& sourceRect, const IntRect& destRect,
58 GC3Denum format, GraphicsContext3D* context)
60 if (m_useMapTexSubImage)
61 uploadWithMapTexSubImage(image, imageRect, sourceRect, destRect, format, context);
63 uploadWithTexSubImage(image, imageRect, sourceRect, destRect, format, context);
66 void LayerTextureSubImage::uploadWithTexSubImage(const uint8_t* image, const IntRect& imageRect,
67 const IntRect& sourceRect, const IntRect& destRect,
68 GC3Denum format, GraphicsContext3D* context)
70 TRACE_EVENT("LayerTextureSubImage::uploadWithTexSubImage", this, 0);
72 m_subImage = adoptArrayPtr(new uint8_t[m_subImageSize.width() * m_subImageSize.height() * 4]);
74 // Offset from image-rect to source-rect.
75 IntPoint offset(sourceRect.x() - imageRect.x(), sourceRect.y() - imageRect.y());
77 const uint8_t* pixelSource;
78 if (imageRect.width() == sourceRect.width() && !offset.x())
79 pixelSource = &image[4 * offset.y() * imageRect.width()];
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);
88 pixelSource = &m_subImage[0];
90 context->texSubImage2D(GraphicsContext3D::TEXTURE_2D, 0, destRect.x(), destRect.y(), destRect.width(), destRect.height(), format, GraphicsContext3D::UNSIGNED_BYTE, pixelSource);
93 void LayerTextureSubImage::uploadWithMapTexSubImage(const uint8_t* image, const IntRect& imageRect,
94 const IntRect& sourceRect, const IntRect& destRect,
95 GC3Denum format, GraphicsContext3D* context)
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());
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));
106 uploadWithTexSubImage(image, imageRect, sourceRect, destRect, format, context);
110 if (imageRect.width() == sourceRect.width() && !offset.x())
111 memcpy(pixelDest, &image[4 * offset.y() * imageRect.width()], imageRect.width() * destRect.height() * 4);
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);
120 extensions->unmapTexSubImage2DCHROMIUM(pixelDest);
123 } // namespace WebCore
125 #endif // USE(ACCELERATED_COMPOSITING)