Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2008, Google Inc. All rights reserved. | 2 * Copyright (c) 2008, Google Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> | 3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
| 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. | 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are | 7 * modification, are permitted provided that the following conditions are |
| 8 * met: | 8 * met: |
| 9 * | 9 * |
| 10 * * Redistributions of source code must retain the above copyright | 10 * * Redistributions of source code must retain the above copyright |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 */ | 31 */ |
| 32 | 32 |
| 33 #include "config.h" | 33 #include "config.h" |
| 34 #include "core/platform/graphics/ImageBuffer.h" | 34 #include "core/platform/graphics/ImageBuffer.h" |
| 35 | 35 |
| 36 #include "core/platform/graphics/BitmapImage.h" | 36 #include "core/platform/graphics/BitmapImage.h" |
| 37 #include "core/platform/graphics/Extensions3D.h" | 37 #include "core/platform/graphics/Extensions3D.h" |
| 38 #include "core/platform/graphics/GaneshUtils.h" | 38 #include "core/platform/graphics/GraphicsContext.h" |
| 39 #include "core/platform/graphics/GraphicsContext3D.h" | 39 #include "core/platform/graphics/GraphicsContext3D.h" |
| 40 #include "core/platform/graphics/gpu/DrawingBuffer.h" | 40 #include "core/platform/graphics/gpu/DrawingBuffer.h" |
| 41 #include "core/platform/graphics/gpu/SharedGraphicsContext3D.h" | 41 #include "core/platform/graphics/gpu/SharedGraphicsContext3D.h" |
| 42 #include "core/platform/graphics/skia/NativeImageSkia.h" | 42 #include "core/platform/graphics/skia/NativeImageSkia.h" |
| 43 #include "core/platform/graphics/skia/SkiaUtils.h" | 43 #include "core/platform/graphics/skia/SkiaUtils.h" |
| 44 #include "core/platform/image-encoders/skia/JPEGImageEncoder.h" | 44 #include "core/platform/image-encoders/skia/JPEGImageEncoder.h" |
| 45 #include "core/platform/image-encoders/skia/PNGImageEncoder.h" | 45 #include "core/platform/image-encoders/skia/PNGImageEncoder.h" |
| 46 #include "core/platform/image-encoders/skia/WEBPImageEncoder.h" | 46 #include "core/platform/image-encoders/skia/WEBPImageEncoder.h" |
| 47 #include "platform/MIMETypeRegistry.h" | 47 #include "platform/MIMETypeRegistry.h" |
| 48 #include "platform/geometry/IntRect.h" | |
| 49 #include "public/platform/Platform.h" | 48 #include "public/platform/Platform.h" |
| 50 #include "skia/ext/platform_canvas.h" | |
| 51 #include "third_party/skia/include/core/SkBitmapDevice.h" | |
| 52 #include "third_party/skia/include/core/SkColorFilter.h" | |
| 53 #include "third_party/skia/include/core/SkColorPriv.h" | |
| 54 #include "third_party/skia/include/core/SkSurface.h" | |
| 55 #include "third_party/skia/include/effects/SkTableColorFilter.h" | 49 #include "third_party/skia/include/effects/SkTableColorFilter.h" |
| 56 #include "third_party/skia/include/gpu/GrContext.h" | |
| 57 #include "third_party/skia/include/gpu/SkGpuDevice.h" | |
| 58 #include "third_party/skia/include/gpu/SkGrPixelRef.h" | |
| 59 #include "wtf/MathExtras.h" | 50 #include "wtf/MathExtras.h" |
| 60 #include "wtf/text/Base64.h" | 51 #include "wtf/text/Base64.h" |
| 61 #include "wtf/text/WTFString.h" | 52 #include "wtf/text/WTFString.h" |
| 62 | 53 |
| 63 using namespace std; | 54 using namespace std; |
| 64 | 55 |
| 65 namespace WebCore { | 56 namespace WebCore { |
| 66 | 57 ImageBuffer::ImageBuffer(PassOwnPtr<ImageBufferSurface> surface) |
| 67 static PassRefPtr<SkCanvas> createAcceleratedCanvas(const IntSize& size, Canvas2 DLayerBridgePtr* outLayerBridge, OpacityMode opacityMode, int msaaSampleCount) | 58 : m_surface(surface) |
| 68 { | 59 { |
| 69 RefPtr<GraphicsContext3D> context3D = SharedGraphicsContext3D::get(); | 60 if (m_surface->isValid() || m_surface->canvas()) { |
|
Stephen White
2013/12/04 21:18:40
Huh?
| |
| 70 if (!context3D) | 61 m_context = adoptPtr(new GraphicsContext(m_surface->canvas())); |
| 71 return 0; | 62 m_context->setCertainlyOpaque(m_surface->opacityMode() == Opaque); |
| 72 Canvas2DLayerBridge::OpacityMode bridgeOpacityMode = opacityMode == Opaque ? Canvas2DLayerBridge::Opaque : Canvas2DLayerBridge::NonOpaque; | 63 m_context->setAccelerated(m_surface->isAccelerated()); |
| 73 *outLayerBridge = Canvas2DLayerBridge::create(context3D.release(), size, bri dgeOpacityMode, msaaSampleCount); | 64 m_context->scale(FloatSize(m_surface->resolutionScale(), m_surface->reso lutionScale())); |
| 74 // If canvas buffer allocation failed, debug build will have asserted | |
| 75 // For release builds, we must verify whether the device has a render target | |
| 76 return (*outLayerBridge) ? (*outLayerBridge)->getCanvas() : 0; | |
| 77 } | |
| 78 | |
| 79 static PassRefPtr<SkCanvas> createTextureBackedCanvas(const IntSize& size) | |
| 80 { | |
| 81 RefPtr<GraphicsContext3D> context3D = SharedGraphicsContext3D::get(); | |
| 82 if (!context3D) | |
| 83 return 0; | |
| 84 GrContext* gr = context3D->grContext(); | |
| 85 if (!gr) | |
| 86 return 0; | |
| 87 SkBitmap* bitmap = new SkBitmap; | |
| 88 if (!bitmap || !ensureTextureBackedSkBitmap(gr, *bitmap, size, kDefault_GrSu rfaceOrigin, kRGBA_8888_GrPixelConfig)) | |
| 89 return 0; | |
| 90 return adoptRef(new SkCanvas(*bitmap)); | |
| 91 } | |
| 92 | |
| 93 static PassRefPtr<SkCanvas> createNonPlatformCanvas(const IntSize& size) | |
| 94 { | |
| 95 SkAutoTUnref<SkBaseDevice> device(new SkBitmapDevice(SkBitmap::kARGB_8888_Co nfig, size.width(), size.height())); | |
| 96 SkPixelRef* pixelRef = device->accessBitmap(false).pixelRef(); | |
| 97 return adoptRef(pixelRef ? new SkCanvas(device) : 0); | |
| 98 } | |
| 99 | |
| 100 PassOwnPtr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const IntSize& size, float resolutionScale, const GraphicsContext* context, bool hasAlpha) | |
| 101 { | |
| 102 bool success = false; | |
| 103 OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, resolutionScale, co ntext, hasAlpha, success)); | |
| 104 if (!success) | |
| 105 return nullptr; | |
| 106 return buf.release(); | |
| 107 } | |
| 108 | |
| 109 PassOwnPtr<ImageBuffer> ImageBuffer::createBufferForTile(const FloatSize& tileSi ze, const FloatSize& clampedTileSize, RenderingMode renderingMode) | |
| 110 { | |
| 111 IntSize imageSize(roundedIntSize(clampedTileSize)); | |
| 112 IntSize unclampedImageSize(roundedIntSize(tileSize)); | |
| 113 | |
| 114 // Don't create empty ImageBuffers. | |
| 115 if (imageSize.isEmpty()) | |
| 116 return nullptr; | |
| 117 | |
| 118 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, renderingMode) ; | |
| 119 if (!image) | |
| 120 return nullptr; | |
| 121 | |
| 122 GraphicsContext* imageContext = image->context(); | |
| 123 ASSERT(imageContext); | |
| 124 | |
| 125 // Compensate rounding effects, as the absolute target rect is using floatin g-point numbers and the image buffer size is integer. | |
| 126 imageContext->scale(FloatSize(unclampedImageSize.width() / tileSize.width(), unclampedImageSize.height() / tileSize.height())); | |
| 127 | |
| 128 return image.release(); | |
| 129 } | |
| 130 | |
| 131 ImageBuffer::ImageBuffer(const IntSize& size, float resolutionScale, const Graph icsContext* compatibleContext, bool hasAlpha, bool& success) | |
| 132 : m_size(size) | |
| 133 , m_logicalSize(size) | |
| 134 , m_resolutionScale(resolutionScale) | |
| 135 { | |
| 136 if (!compatibleContext) { | |
| 137 success = false; | |
| 138 return; | |
| 139 } | 65 } |
| 140 | |
| 141 SkAutoTUnref<SkBaseDevice> device(compatibleContext->createCompatibleDevice( size, hasAlpha)); | |
| 142 if (!device.get()) { | |
| 143 success = false; | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 SkPixelRef* pixelRef = device->accessBitmap(false).pixelRef(); | |
| 148 if (!pixelRef) { | |
| 149 success = false; | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 m_canvas = adoptRef(new SkCanvas(device)); | |
| 154 m_context = adoptPtr(new GraphicsContext(m_canvas.get())); | |
| 155 m_context->setCertainlyOpaque(!hasAlpha); | |
| 156 m_context->scale(FloatSize(m_resolutionScale, m_resolutionScale)); | |
| 157 | |
| 158 success = true; | |
| 159 } | |
| 160 | |
| 161 ImageBuffer::ImageBuffer(const IntSize& size, float resolutionScale, RenderingMo de renderingMode, OpacityMode opacityMode, int acceleratedSampleCount, bool& suc cess) | |
| 162 : m_size(size) | |
| 163 , m_logicalSize(size) | |
| 164 , m_resolutionScale(resolutionScale) | |
| 165 { | |
| 166 if (renderingMode == Accelerated) { | |
| 167 m_canvas = createAcceleratedCanvas(size, &m_layerBridge, opacityMode, ac celeratedSampleCount); | |
| 168 if (!m_canvas) | |
| 169 renderingMode = UnacceleratedNonPlatformBuffer; | |
| 170 } | |
| 171 | |
| 172 if (renderingMode == TextureBacked) { | |
| 173 m_canvas = createTextureBackedCanvas(size); | |
| 174 if (!m_canvas) | |
| 175 renderingMode = UnacceleratedNonPlatformBuffer; | |
| 176 } | |
| 177 | |
| 178 if (renderingMode == UnacceleratedNonPlatformBuffer) | |
| 179 m_canvas = createNonPlatformCanvas(size); | |
| 180 | |
| 181 if (!m_canvas) | |
| 182 m_canvas = adoptRef(skia::TryCreateBitmapCanvas(size.width(), size.heigh t(), false)); | |
| 183 | |
| 184 if (!m_canvas) { | |
| 185 success = false; | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 m_context = adoptPtr(new GraphicsContext(m_canvas.get())); | |
| 190 m_context->setCertainlyOpaque(opacityMode == Opaque); | |
| 191 m_context->setAccelerated(renderingMode == Accelerated); | |
| 192 m_context->scale(FloatSize(m_resolutionScale, m_resolutionScale)); | |
| 193 | |
| 194 // Clear the background transparent or opaque, as required. It would be nice if this wasn't | |
| 195 // required, but the canvas is currently filled with the magic transparency | |
| 196 // color. Can we have another way to manage this? | |
| 197 if (renderingMode != TextureBacked) { | |
| 198 if (opacityMode == Opaque) | |
| 199 m_canvas->drawARGB(255, 0, 0, 0, SkXfermode::kSrc_Mode); | |
| 200 else | |
| 201 m_canvas->drawARGB(0, 0, 0, 0, SkXfermode::kClear_Mode); | |
| 202 } | |
| 203 | |
| 204 success = true; | |
| 205 } | 66 } |
| 206 | 67 |
| 207 ImageBuffer::~ImageBuffer() | 68 ImageBuffer::~ImageBuffer() |
| 208 { | 69 { |
| 209 } | 70 } |
| 210 | 71 |
| 211 GraphicsContext* ImageBuffer::context() const | 72 GraphicsContext* ImageBuffer::context() const |
| 212 { | 73 { |
| 213 if (m_layerBridge) { | 74 m_surface->aboutToUse(); |
| 214 // We're using context acquisition as a signal that someone is about to render into our buffer and we need | |
| 215 // to be ready. This isn't logically const-correct, hence the cast. | |
| 216 const_cast<Canvas2DLayerBridge*>(m_layerBridge.get())->contextAcquired() ; | |
| 217 } | |
| 218 return m_context.get(); | 75 return m_context.get(); |
| 219 } | 76 } |
| 220 | 77 |
| 221 | 78 |
| 222 bool ImageBuffer::isValid() const | 79 bool ImageBuffer::isValid() const |
| 223 { | 80 { |
| 224 if (m_layerBridge) | 81 return m_surface->isValid(); |
| 225 return const_cast<Canvas2DLayerBridge*>(m_layerBridge.get())->isValid(); | |
| 226 return true; | |
| 227 } | 82 } |
| 228 | 83 |
| 229 static SkBitmap deepSkBitmapCopy(const SkBitmap& bitmap) | 84 static SkBitmap deepSkBitmapCopy(const SkBitmap& bitmap) |
| 230 { | 85 { |
| 231 SkBitmap tmp; | 86 SkBitmap tmp; |
| 232 if (!bitmap.deepCopyTo(&tmp, bitmap.config())) | 87 if (!bitmap.deepCopyTo(&tmp, bitmap.config())) |
| 233 bitmap.copyTo(&tmp, bitmap.config()); | 88 bitmap.copyTo(&tmp, bitmap.config()); |
| 234 | 89 |
| 235 return tmp; | 90 return tmp; |
| 236 } | 91 } |
| 237 | 92 |
| 238 PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBeh avior) const | 93 PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBeh avior) const |
| 239 { | 94 { |
| 240 if (!isValid()) | 95 if (!isValid()) |
| 241 return BitmapImage::create(NativeImageSkia::create()); | 96 return BitmapImage::create(NativeImageSkia::create()); |
| 242 | 97 |
| 243 const SkBitmap& bitmap = *context()->bitmap(); | 98 const SkBitmap& bitmap = m_surface->bitmap(); |
| 244 // FIXME: Start honoring ScaleBehavior to scale 2x buffers down to 1x. | 99 return BitmapImage::create(NativeImageSkia::create(copyBehavior == CopyBacki ngStore ? deepSkBitmapCopy(bitmap) : bitmap, m_surface->resolutionScale())); |
| 245 return BitmapImage::create(NativeImageSkia::create(copyBehavior == CopyBacki ngStore ? deepSkBitmapCopy(bitmap) : bitmap, m_resolutionScale)); | |
| 246 } | 100 } |
| 247 | 101 |
| 248 BackingStoreCopy ImageBuffer::fastCopyImageMode() | 102 BackingStoreCopy ImageBuffer::fastCopyImageMode() |
| 249 { | 103 { |
| 250 return DontCopyBackingStore; | 104 return DontCopyBackingStore; |
| 251 } | 105 } |
| 252 | 106 |
| 253 blink::WebLayer* ImageBuffer::platformLayer() const | 107 blink::WebLayer* ImageBuffer::platformLayer() const |
| 254 { | 108 { |
| 255 return m_layerBridge ? m_layerBridge->layer() : 0; | 109 return m_surface->layer(); |
| 256 } | 110 } |
| 257 | 111 |
| 258 bool ImageBuffer::copyToPlatformTexture(GraphicsContext3D& context, Platform3DOb ject texture, GC3Denum internalFormat, GC3Denum destType, GC3Dint level, bool pr emultiplyAlpha, bool flipY) | 112 bool ImageBuffer::copyToPlatformTexture(GraphicsContext3D& context, Platform3DOb ject texture, GC3Denum internalFormat, GC3Denum destType, GC3Dint level, bool pr emultiplyAlpha, bool flipY) |
| 259 { | 113 { |
| 260 if (!m_layerBridge || !platformLayer() || !isValid()) | 114 if (!m_surface->isAccelerated() || !platformLayer() || !isValid()) |
| 261 return false; | 115 return false; |
| 262 | 116 |
| 263 Platform3DObject sourceTexture = m_layerBridge->backBufferTexture(); | |
| 264 | |
| 265 if (!context.makeContextCurrent()) | 117 if (!context.makeContextCurrent()) |
| 266 return false; | 118 return false; |
| 267 | 119 |
| 268 Extensions3D* extensions = context.extensions(); | 120 Extensions3D* extensions = context.extensions(); |
| 269 if (!extensions->supports("GL_CHROMIUM_copy_texture") || !extensions->suppor ts("GL_CHROMIUM_flipy") | 121 if (!extensions->supports("GL_CHROMIUM_copy_texture") || !extensions->suppor ts("GL_CHROMIUM_flipy") |
| 270 || !extensions->canUseCopyTextureCHROMIUM(internalFormat, destType, leve l)) | 122 || !extensions->canUseCopyTextureCHROMIUM(internalFormat, destType, leve l)) |
| 271 return false; | 123 return false; |
| 272 | 124 |
| 273 // The canvas is stored in a premultiplied format, so unpremultiply if neces sary. | 125 // The canvas is stored in a premultiplied format, so unpremultiply if neces sary. |
| 274 context.pixelStorei(Extensions3D::UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, !prem ultiplyAlpha); | 126 context.pixelStorei(Extensions3D::UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, !prem ultiplyAlpha); |
| 275 | 127 |
| 276 // The canvas is stored in an inverted position, so the flip semantics are r eversed. | 128 // The canvas is stored in an inverted position, so the flip semantics are r eversed. |
| 277 context.pixelStorei(Extensions3D::UNPACK_FLIP_Y_CHROMIUM, !flipY); | 129 context.pixelStorei(Extensions3D::UNPACK_FLIP_Y_CHROMIUM, !flipY); |
| 278 | 130 extensions->copyTextureCHROMIUM(GraphicsContext3D::TEXTURE_2D, getBackingTex ture(), texture, level, internalFormat, destType); |
| 279 extensions->copyTextureCHROMIUM(GraphicsContext3D::TEXTURE_2D, sourceTexture , texture, level, internalFormat, destType); | |
| 280 | 131 |
| 281 context.pixelStorei(Extensions3D::UNPACK_FLIP_Y_CHROMIUM, false); | 132 context.pixelStorei(Extensions3D::UNPACK_FLIP_Y_CHROMIUM, false); |
| 282 context.pixelStorei(Extensions3D::UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, false ); | 133 context.pixelStorei(Extensions3D::UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, false ); |
| 283 context.flush(); | 134 context.flush(); |
| 284 return true; | 135 return true; |
| 285 } | 136 } |
| 286 | 137 |
| 287 static bool drawNeedsCopy(GraphicsContext* src, GraphicsContext* dst) | 138 static bool drawNeedsCopy(GraphicsContext* src, GraphicsContext* dst) |
| 288 { | 139 { |
| 140 ASSERT(dst); | |
| 289 return (src == dst); | 141 return (src == dst); |
| 290 } | 142 } |
| 291 | 143 |
| 292 Platform3DObject ImageBuffer::getBackingTexture() | 144 Platform3DObject ImageBuffer::getBackingTexture() |
| 293 { | 145 { |
| 294 if (!m_context || !m_context->bitmap()) | 146 return m_surface->getBackingTexture(); |
| 295 return 0; | |
| 296 const SkBitmap& bitmap = *m_context->bitmap(); | |
| 297 if (bitmap.getTexture()) | |
| 298 return (bitmap.getTexture())->getTextureHandle(); | |
| 299 return 0; | |
| 300 } | 147 } |
| 301 | 148 |
| 302 bool ImageBuffer::copyRenderingResultsFromDrawingBuffer(DrawingBuffer* drawingBu ffer) | 149 bool ImageBuffer::copyRenderingResultsFromDrawingBuffer(DrawingBuffer* drawingBu ffer) |
| 303 { | 150 { |
| 304 if (!drawingBuffer) | 151 if (!drawingBuffer) |
| 305 return false; | 152 return false; |
| 306 RefPtr<GraphicsContext3D> context3D = SharedGraphicsContext3D::get(); | 153 RefPtr<GraphicsContext3D> context3D = SharedGraphicsContext3D::get(); |
| 307 Platform3DObject tex = getBackingTexture(); | 154 Platform3DObject tex = m_surface->getBackingTexture(); |
| 308 if (!context3D || !tex) | 155 if (!context3D || !tex) |
| 309 return false; | 156 return false; |
| 310 | 157 |
| 311 return drawingBuffer->copyToPlatformTexture(*(context3D.get()), tex, Graphic sContext3D::RGBA, | 158 return drawingBuffer->copyToPlatformTexture(*(context3D.get()), tex, Graphic sContext3D::RGBA, |
| 312 GraphicsContext3D::UNSIGNED_BYTE, 0, true, false); | 159 GraphicsContext3D::UNSIGNED_BYTE, 0, true, false); |
| 313 } | 160 } |
| 314 | 161 |
| 315 void ImageBuffer::draw(GraphicsContext* context, const FloatRect& destRect, cons t FloatRect& srcRect, | 162 void ImageBuffer::draw(GraphicsContext* context, const FloatRect& destRect, cons t FloatRect& srcRect, |
| 316 CompositeOperator op, blink::WebBlendMode blendMode, bool useLowQualityScale ) | 163 CompositeOperator op, blink::WebBlendMode blendMode, bool useLowQualityScale ) |
| 317 { | 164 { |
| 318 if (!isValid()) | 165 if (!isValid()) |
| 319 return; | 166 return; |
| 320 | 167 |
| 321 const SkBitmap& bitmap = *m_context->bitmap(); | 168 const SkBitmap& bitmap = m_surface->bitmap(); |
| 322 RefPtr<Image> image = BitmapImage::create(NativeImageSkia::create(drawNeedsC opy(m_context.get(), context) ? deepSkBitmapCopy(bitmap) : bitmap)); | 169 RefPtr<Image> image = BitmapImage::create(NativeImageSkia::create(drawNeedsC opy(m_context.get(), context) ? deepSkBitmapCopy(bitmap) : bitmap)); |
| 323 context->drawImage(image.get(), destRect, srcRect, op, blendMode, DoNotRespe ctImageOrientation, useLowQualityScale); | 170 context->drawImage(image.get(), destRect, srcRect, op, blendMode, DoNotRespe ctImageOrientation, useLowQualityScale); |
| 324 } | 171 } |
| 325 | 172 |
| 326 void ImageBuffer::drawPattern(GraphicsContext* context, const FloatRect& srcRect , const FloatSize& scale, | 173 void ImageBuffer::drawPattern(GraphicsContext* context, const FloatRect& srcRect , const FloatSize& scale, |
| 327 const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect, bl ink::WebBlendMode blendMode, const IntSize& repeatSpacing) | 174 const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect, bl ink::WebBlendMode blendMode, const IntSize& repeatSpacing) |
| 328 { | 175 { |
| 329 if (!isValid()) | 176 if (!isValid()) |
| 330 return; | 177 return; |
| 331 | 178 |
| 332 const SkBitmap& bitmap = *m_context->bitmap(); | 179 const SkBitmap& bitmap = m_surface->bitmap(); |
| 333 RefPtr<Image> image = BitmapImage::create(NativeImageSkia::create(drawNeedsC opy(m_context.get(), context) ? deepSkBitmapCopy(bitmap) : bitmap)); | 180 RefPtr<Image> image = BitmapImage::create(NativeImageSkia::create(drawNeedsC opy(m_context.get(), context) ? deepSkBitmapCopy(bitmap) : bitmap)); |
| 334 image->drawPattern(context, srcRect, scale, phase, op, destRect, blendMode, repeatSpacing); | 181 image->drawPattern(context, srcRect, scale, phase, op, destRect, blendMode, repeatSpacing); |
| 335 } | 182 } |
| 336 | 183 |
| 337 static const Vector<uint8_t>& getLinearRgbLUT() | 184 static const Vector<uint8_t>& getLinearRgbLUT() |
| 338 { | 185 { |
| 339 DEFINE_STATIC_LOCAL(Vector<uint8_t>, linearRgbLUT, ()); | 186 DEFINE_STATIC_LOCAL(Vector<uint8_t>, linearRgbLUT, ()); |
| 340 if (linearRgbLUT.isEmpty()) { | 187 if (linearRgbLUT.isEmpty()) { |
| 341 linearRgbLUT.reserveCapacity(256); | 188 linearRgbLUT.reserveCapacity(256); |
| 342 for (unsigned i = 0; i < 256; i++) { | 189 for (unsigned i = 0; i < 256; i++) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 373 | 220 |
| 374 // only sRGB <-> linearRGB are supported at the moment | 221 // only sRGB <-> linearRGB are supported at the moment |
| 375 if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDevi ceRGB) | 222 if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDevi ceRGB) |
| 376 || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceD eviceRGB)) | 223 || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceD eviceRGB)) |
| 377 return; | 224 return; |
| 378 | 225 |
| 379 // FIXME: Disable color space conversions on accelerated canvases (for now). | 226 // FIXME: Disable color space conversions on accelerated canvases (for now). |
| 380 if (context()->isAccelerated() || !isValid()) | 227 if (context()->isAccelerated() || !isValid()) |
| 381 return; | 228 return; |
| 382 | 229 |
| 383 const SkBitmap& bitmap = *context()->bitmap(); | 230 const SkBitmap& bitmap = m_surface->bitmap(); |
| 384 if (bitmap.isNull()) | 231 if (bitmap.isNull()) |
| 385 return; | 232 return; |
| 386 | 233 |
| 387 const Vector<uint8_t>& lookUpTable = dstColorSpace == ColorSpaceLinearRGB ? | 234 const Vector<uint8_t>& lookUpTable = dstColorSpace == ColorSpaceLinearRGB ? |
| 388 getLinearRgbLUT() : getDeviceRgbLUT(); | 235 getLinearRgbLUT() : getDeviceRgbLUT(); |
| 389 | 236 |
| 390 ASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config); | 237 ASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config); |
| 238 IntSize size = m_surface->size(); | |
| 391 SkAutoLockPixels bitmapLock(bitmap); | 239 SkAutoLockPixels bitmapLock(bitmap); |
| 392 for (int y = 0; y < m_size.height(); ++y) { | 240 for (int y = 0; y < size.height(); ++y) { |
| 393 uint32_t* srcRow = bitmap.getAddr32(0, y); | 241 uint32_t* srcRow = bitmap.getAddr32(0, y); |
| 394 for (int x = 0; x < m_size.width(); ++x) { | 242 for (int x = 0; x < size.width(); ++x) { |
| 395 SkColor color = SkPMColorToColor(srcRow[x]); | 243 SkColor color = SkPMColorToColor(srcRow[x]); |
| 396 srcRow[x] = SkPreMultiplyARGB( | 244 srcRow[x] = SkPreMultiplyARGB( |
| 397 SkColorGetA(color), | 245 SkColorGetA(color), |
| 398 lookUpTable[SkColorGetR(color)], | 246 lookUpTable[SkColorGetR(color)], |
| 399 lookUpTable[SkColorGetG(color)], | 247 lookUpTable[SkColorGetG(color)], |
| 400 lookUpTable[SkColorGetB(color)]); | 248 lookUpTable[SkColorGetB(color)]); |
| 401 } | 249 } |
| 402 } | 250 } |
| 403 } | 251 } |
| 404 | 252 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 450 config8888 = SkCanvas::kRGBA_Unpremul_Config8888; | 298 config8888 = SkCanvas::kRGBA_Unpremul_Config8888; |
| 451 | 299 |
| 452 context->readPixels(&destBitmap, rect.x(), rect.y(), config8888); | 300 context->readPixels(&destBitmap, rect.x(), rect.y(), config8888); |
| 453 return result.release(); | 301 return result.release(); |
| 454 } | 302 } |
| 455 | 303 |
| 456 PassRefPtr<Uint8ClampedArray> ImageBuffer::getUnmultipliedImageData(const IntRec t& rect, CoordinateSystem) const | 304 PassRefPtr<Uint8ClampedArray> ImageBuffer::getUnmultipliedImageData(const IntRec t& rect, CoordinateSystem) const |
| 457 { | 305 { |
| 458 if (!isValid()) | 306 if (!isValid()) |
| 459 return Uint8ClampedArray::create(rect.width() * rect.height() * 4); | 307 return Uint8ClampedArray::create(rect.width() * rect.height() * 4); |
| 460 return getImageData<Unmultiplied>(rect, context(), m_size); | 308 return getImageData<Unmultiplied>(rect, context(), m_surface->size()); |
| 461 } | 309 } |
| 462 | 310 |
| 463 PassRefPtr<Uint8ClampedArray> ImageBuffer::getPremultipliedImageData(const IntRe ct& rect, CoordinateSystem) const | 311 PassRefPtr<Uint8ClampedArray> ImageBuffer::getPremultipliedImageData(const IntRe ct& rect, CoordinateSystem) const |
| 464 { | 312 { |
| 465 if (!isValid()) | 313 if (!isValid()) |
| 466 return Uint8ClampedArray::create(rect.width() * rect.height() * 4); | 314 return Uint8ClampedArray::create(rect.width() * rect.height() * 4); |
| 467 return getImageData<Premultiplied>(rect, context(), m_size); | 315 return getImageData<Premultiplied>(rect, context(), m_surface->size()); |
| 468 } | 316 } |
| 469 | 317 |
| 470 void ImageBuffer::putByteArray(Multiply multiplied, Uint8ClampedArray* source, c onst IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem) | 318 void ImageBuffer::putByteArray(Multiply multiplied, Uint8ClampedArray* source, c onst IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem) |
| 471 { | 319 { |
| 472 if (!isValid()) | 320 if (!isValid()) |
| 473 return; | 321 return; |
| 474 | 322 |
| 475 ASSERT(sourceRect.width() > 0); | 323 ASSERT(sourceRect.width() > 0); |
| 476 ASSERT(sourceRect.height() > 0); | 324 ASSERT(sourceRect.height() > 0); |
| 477 | 325 |
| 478 int originX = sourceRect.x(); | 326 int originX = sourceRect.x(); |
| 479 int destX = destPoint.x() + sourceRect.x(); | 327 int destX = destPoint.x() + sourceRect.x(); |
| 480 ASSERT(destX >= 0); | 328 ASSERT(destX >= 0); |
| 481 ASSERT(destX < m_size.width()); | 329 ASSERT(destX < m_surface->size().width()); |
| 482 ASSERT(originX >= 0); | 330 ASSERT(originX >= 0); |
| 483 ASSERT(originX < sourceRect.maxX()); | 331 ASSERT(originX < sourceRect.maxX()); |
| 484 | 332 |
| 485 int endX = destPoint.x() + sourceRect.maxX(); | 333 int endX = destPoint.x() + sourceRect.maxX(); |
| 486 ASSERT(endX <= m_size.width()); | 334 ASSERT(endX <= m_surface->size().width()); |
| 487 | 335 |
| 488 int numColumns = endX - destX; | 336 int numColumns = endX - destX; |
| 489 | 337 |
| 490 int originY = sourceRect.y(); | 338 int originY = sourceRect.y(); |
| 491 int destY = destPoint.y() + sourceRect.y(); | 339 int destY = destPoint.y() + sourceRect.y(); |
| 492 ASSERT(destY >= 0); | 340 ASSERT(destY >= 0); |
| 493 ASSERT(destY < m_size.height()); | 341 ASSERT(destY < m_surface->size().height()); |
| 494 ASSERT(originY >= 0); | 342 ASSERT(originY >= 0); |
| 495 ASSERT(originY < sourceRect.maxY()); | 343 ASSERT(originY < sourceRect.maxY()); |
| 496 | 344 |
| 497 int endY = destPoint.y() + sourceRect.maxY(); | 345 int endY = destPoint.y() + sourceRect.maxY(); |
| 498 ASSERT(endY <= m_size.height()); | 346 ASSERT(endY <= m_surface->size().height()); |
| 499 int numRows = endY - destY; | 347 int numRows = endY - destY; |
| 500 | 348 |
| 501 unsigned srcBytesPerRow = 4 * sourceSize.width(); | 349 unsigned srcBytesPerRow = 4 * sourceSize.width(); |
| 502 SkBitmap srcBitmap; | 350 SkBitmap srcBitmap; |
| 503 srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, numColumns, numRows, srcByt esPerRow); | 351 srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, numColumns, numRows, srcByt esPerRow); |
| 504 srcBitmap.setPixels(source->data() + originY * srcBytesPerRow + originX * 4) ; | 352 srcBitmap.setPixels(source->data() + originY * srcBytesPerRow + originX * 4) ; |
| 505 | 353 |
| 506 SkCanvas::Config8888 config8888; | 354 SkCanvas::Config8888 config8888; |
| 507 if (multiplied == Premultiplied) | 355 if (multiplied == Premultiplied) |
| 508 config8888 = SkCanvas::kRGBA_Premul_Config8888; | 356 config8888 = SkCanvas::kRGBA_Premul_Config8888; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 536 } | 384 } |
| 537 | 385 |
| 538 return true; | 386 return true; |
| 539 } | 387 } |
| 540 | 388 |
| 541 String ImageBuffer::toDataURL(const String& mimeType, const double* quality, Coo rdinateSystem) const | 389 String ImageBuffer::toDataURL(const String& mimeType, const double* quality, Coo rdinateSystem) const |
| 542 { | 390 { |
| 543 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | 391 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 544 | 392 |
| 545 Vector<char> encodedImage; | 393 Vector<char> encodedImage; |
| 546 if (!isValid() || !encodeImage(*context()->bitmap(), mimeType, quality, &enc odedImage)) | 394 if (!isValid() || !encodeImage(m_surface->bitmap(), mimeType, quality, &enco dedImage)) |
| 547 return "data:,"; | 395 return "data:,"; |
| 548 Vector<char> base64Data; | 396 Vector<char> base64Data; |
| 549 base64Encode(encodedImage, base64Data); | 397 base64Encode(encodedImage, base64Data); |
| 550 | 398 |
| 551 return "data:" + mimeType + ";base64," + base64Data; | 399 return "data:" + mimeType + ";base64," + base64Data; |
| 552 } | 400 } |
| 553 | 401 |
| 554 String ImageDataToDataURL(const ImageDataBuffer& imageData, const String& mimeTy pe, const double* quality) | 402 String ImageDataToDataURL(const ImageDataBuffer& imageData, const String& mimeTy pe, const double* quality) |
| 555 { | 403 { |
| 556 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | 404 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 557 | 405 |
| 558 Vector<char> encodedImage; | 406 Vector<char> encodedImage; |
| 559 if (!encodeImage(imageData, mimeType, quality, &encodedImage)) | 407 if (!encodeImage(imageData, mimeType, quality, &encodedImage)) |
| 560 return "data:,"; | 408 return "data:,"; |
| 561 | 409 |
| 562 Vector<char> base64Data; | 410 Vector<char> base64Data; |
| 563 base64Encode(encodedImage, base64Data); | 411 base64Encode(encodedImage, base64Data); |
| 564 | 412 |
| 565 return "data:" + mimeType + ";base64," + base64Data; | 413 return "data:" + mimeType + ";base64," + base64Data; |
| 566 } | 414 } |
| 567 | 415 |
| 568 } // namespace WebCore | 416 } // namespace WebCore |
| OLD | NEW |