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 22 matching lines...) Expand all Loading... | |
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/html/ImageData.h" | 36 #include "core/html/ImageData.h" |
37 #include "core/platform/MIMETypeRegistry.h" | 37 #include "core/platform/MIMETypeRegistry.h" |
38 #include "core/platform/graphics/BitmapImage.h" | 38 #include "core/platform/graphics/BitmapImage.h" |
39 #include "core/platform/graphics/Extensions3D.h" | 39 #include "core/platform/graphics/Extensions3D.h" |
40 #include "core/platform/graphics/GraphicsContext.h" | 40 #include "core/platform/graphics/GraphicsContext.h" |
41 #include "core/platform/graphics/GraphicsContext3D.h" | 41 #include "core/platform/graphics/GraphicsContext3D.h" |
42 #include "core/platform/graphics/IntRect.h" | 42 #include "core/platform/graphics/IntRect.h" |
43 #include "core/platform/graphics/ReadOnlyBitmapImage.h" | |
43 #include "core/platform/graphics/chromium/Canvas2DLayerBridge.h" | 44 #include "core/platform/graphics/chromium/Canvas2DLayerBridge.h" |
44 #include "core/platform/graphics/gpu/SharedGraphicsContext3D.h" | 45 #include "core/platform/graphics/gpu/SharedGraphicsContext3D.h" |
45 #include "core/platform/graphics/skia/NativeImageSkia.h" | 46 #include "core/platform/graphics/skia/NativeImageSkia.h" |
46 #include "core/platform/graphics/skia/SkiaUtils.h" | 47 #include "core/platform/graphics/skia/SkiaUtils.h" |
47 #include "core/platform/image-encoders/skia/JPEGImageEncoder.h" | 48 #include "core/platform/image-encoders/skia/JPEGImageEncoder.h" |
48 #include "core/platform/image-encoders/skia/PNGImageEncoder.h" | 49 #include "core/platform/image-encoders/skia/PNGImageEncoder.h" |
49 #include "core/platform/image-encoders/skia/WEBPImageEncoder.h" | 50 #include "core/platform/image-encoders/skia/WEBPImageEncoder.h" |
50 #include "public/platform/Platform.h" | 51 #include "public/platform/Platform.h" |
51 #include "skia/ext/platform_canvas.h" | 52 #include "skia/ext/platform_canvas.h" |
52 #include "third_party/skia/include/core/SkColorPriv.h" | 53 #include "third_party/skia/include/core/SkColorPriv.h" |
53 #include "third_party/skia/include/core/SkSurface.h" | 54 #include "third_party/skia/include/core/SkSurface.h" |
54 #include "third_party/skia/include/gpu/GrContext.h" | 55 #include "third_party/skia/include/gpu/GrContext.h" |
55 #include "third_party/skia/include/gpu/SkGpuDevice.h" | 56 #include "third_party/skia/include/gpu/SkGpuDevice.h" |
56 #include "wtf/MathExtras.h" | 57 #include "wtf/MathExtras.h" |
57 #include "wtf/text/Base64.h" | 58 #include "wtf/text/Base64.h" |
58 #include "wtf/text/WTFString.h" | 59 #include "wtf/text/WTFString.h" |
59 | 60 |
60 using namespace std; | 61 using namespace std; |
61 | 62 |
62 namespace WebCore { | 63 namespace WebCore { |
63 | 64 |
64 static SkCanvas* createAcceleratedCanvas(const IntSize& size, OwnPtr<Canvas2DLay erBridge>* outLayerBridge, OpacityMode opacityMode) | 65 void ImageBuffer::createAcceleratedCanvas(const IntSize& size, OwnPtr<Canvas2DLa yerBridge>* outLayerBridge, OpacityMode opacityMode) |
Justin Novosad
2013/08/19 20:12:43
This clashes with a refactoring I have in flight.
Stephen White
2013/08/19 20:21:21
If you don't need the SkCanvas* return value anymo
danakj
2013/08/19 20:22:33
SkCanvas is refcounted. Should it not go in a RefP
| |
65 { | 66 { |
66 RefPtr<GraphicsContext3D> context3D = SharedGraphicsContext3D::get(); | 67 RefPtr<GraphicsContext3D> context3D = SharedGraphicsContext3D::get(); |
67 if (!context3D) | 68 if (!context3D) |
68 return 0; | 69 return; |
69 Canvas2DLayerBridge::OpacityMode bridgeOpacityMode = opacityMode == Opaque ? Canvas2DLayerBridge::Opaque : Canvas2DLayerBridge::NonOpaque; | 70 Canvas2DLayerBridge::OpacityMode bridgeOpacityMode = opacityMode == Opaque ? Canvas2DLayerBridge::Opaque : Canvas2DLayerBridge::NonOpaque; |
70 *outLayerBridge = Canvas2DLayerBridge::create(context3D.release(), size, bri dgeOpacityMode); | 71 *outLayerBridge = Canvas2DLayerBridge::create(context3D.release(), size, bri dgeOpacityMode); |
72 | |
73 // FIXME: refactor Canvas2DLayerBridge so that SkSurface is accessible. | |
74 SkSurface* surface = 0; | |
75 m_surface = adoptPtr(surface); | |
76 | |
71 // If canvas buffer allocation failed, debug build will have asserted | 77 // If canvas buffer allocation failed, debug build will have asserted |
72 // For release builds, we must verify whether the device has a render target | 78 // For release builds, we must verify whether the device has a render target |
73 return (*outLayerBridge) ? (*outLayerBridge)->getCanvas() : 0; | 79 SkCanvas* canvas = (*outLayerBridge) ? (*outLayerBridge)->getCanvas() : 0; |
80 m_canvas = adoptPtr(canvas); | |
74 } | 81 } |
75 | 82 |
76 static SkCanvas* createNonPlatformCanvas(const IntSize& size) | 83 void ImageBuffer::createNonPlatformCanvas(const IntSize& size) |
77 { | 84 { |
78 SkAutoTUnref<SkDevice> device(new SkDevice(SkBitmap::kARGB_8888_Config, size .width(), size.height())); | 85 SkImage::Info info; |
79 SkPixelRef* pixelRef = device->accessBitmap(false).pixelRef(); | 86 info.fWidth = size.width(); |
80 return pixelRef ? new SkCanvas(device) : 0; | 87 info.fHeight = size.height(); |
88 info.fColorType = SkImage::kRGBA_8888_ColorType; | |
89 info.fAlphaType = SkImage::kPremul_AlphaType; | |
90 m_surface = adoptPtr(SkSurface::NewRaster(info)); | |
91 if (!m_surface) | |
92 return; | |
93 m_canvas = adoptPtr(m_surface->getCanvas()); | |
81 } | 94 } |
82 | 95 |
83 PassOwnPtr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const IntSize& size, float resolutionScale, const GraphicsContext* context, bool hasAlpha) | 96 PassOwnPtr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const IntSize& size, float resolutionScale, const GraphicsContext* context, bool hasAlpha) |
84 { | 97 { |
85 bool success = false; | 98 bool success = false; |
86 OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, resolutionScale, co ntext, hasAlpha, success)); | 99 OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, resolutionScale, co ntext, hasAlpha, success)); |
87 if (!success) | 100 if (!success) |
88 return nullptr; | 101 return nullptr; |
89 return buf.release(); | 102 return buf.release(); |
90 } | 103 } |
(...skipping 27 matching lines...) Expand all Loading... | |
118 | 131 |
119 success = true; | 132 success = true; |
120 } | 133 } |
121 | 134 |
122 ImageBuffer::ImageBuffer(const IntSize& size, float resolutionScale, RenderingMo de renderingMode, OpacityMode opacityMode, bool& success) | 135 ImageBuffer::ImageBuffer(const IntSize& size, float resolutionScale, RenderingMo de renderingMode, OpacityMode opacityMode, bool& success) |
123 : m_size(size) | 136 : m_size(size) |
124 , m_logicalSize(size) | 137 , m_logicalSize(size) |
125 , m_resolutionScale(resolutionScale) | 138 , m_resolutionScale(resolutionScale) |
126 { | 139 { |
127 if (renderingMode == Accelerated) { | 140 if (renderingMode == Accelerated) { |
128 m_canvas = adoptPtr(createAcceleratedCanvas(size, &m_layerBridge, opacit yMode)); | 141 createAcceleratedCanvas(size, &m_layerBridge, opacityMode); |
129 if (!m_canvas) | 142 if (!m_canvas) |
130 renderingMode = UnacceleratedNonPlatformBuffer; | 143 renderingMode = UnacceleratedNonPlatformBuffer; |
131 } | 144 } |
132 | 145 |
133 if (renderingMode == UnacceleratedNonPlatformBuffer) | 146 if (renderingMode == UnacceleratedNonPlatformBuffer) |
134 m_canvas = adoptPtr(createNonPlatformCanvas(size)); | 147 createNonPlatformCanvas(size); |
135 | 148 |
136 if (!m_canvas) | 149 if (!m_canvas) |
137 m_canvas = adoptPtr(skia::TryCreateBitmapCanvas(size.width(), size.heigh t(), false)); | 150 m_canvas = adoptPtr(skia::TryCreateBitmapCanvas(size.width(), size.heigh t(), false)); |
138 | 151 |
139 if (!m_canvas) { | 152 if (!m_canvas) { |
140 success = false; | 153 success = false; |
141 return; | 154 return; |
142 } | 155 } |
143 | 156 |
144 m_context = adoptPtr(new GraphicsContext(m_canvas.get())); | 157 m_context = adoptPtr(new GraphicsContext(m_canvas.get())); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 | 194 |
182 static SkBitmap deepSkBitmapCopy(const SkBitmap& bitmap) | 195 static SkBitmap deepSkBitmapCopy(const SkBitmap& bitmap) |
183 { | 196 { |
184 SkBitmap tmp; | 197 SkBitmap tmp; |
185 if (!bitmap.deepCopyTo(&tmp, bitmap.config())) | 198 if (!bitmap.deepCopyTo(&tmp, bitmap.config())) |
186 bitmap.copyTo(&tmp, bitmap.config()); | 199 bitmap.copyTo(&tmp, bitmap.config()); |
187 | 200 |
188 return tmp; | 201 return tmp; |
189 } | 202 } |
190 | 203 |
204 PassRefPtr<Image> ImageBuffer::imageSnapshot() const | |
205 { | |
206 // A canvas without a surface indicates that a platform buffer is being used . imageSnapshot() on a platform | |
207 // buffer is not supported. | |
208 ASSERT(!m_canvas); | |
209 | |
210 if (!isValid()) | |
211 return BitmapImage::create(NativeImageSkia::create()); | |
212 | |
213 // FIXME: Case where canvas is acclerated. This should be removed once Image Buffer holds a ref to the SkSurface. | |
214 if (!m_surface) | |
215 return copyImage(CopyBackingStore); | |
216 | |
217 return ReadOnlyBitmapImage::create(m_surface->newImageSnapshot()); | |
218 } | |
219 | |
191 PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBeh avior) const | 220 PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBeh avior) const |
192 { | 221 { |
193 if (!isValid()) | 222 if (!isValid()) |
194 return BitmapImage::create(NativeImageSkia::create()); | 223 return BitmapImage::create(NativeImageSkia::create()); |
195 | 224 |
196 const SkBitmap& bitmap = *context()->bitmap(); | 225 const SkBitmap& bitmap = *context()->bitmap(); |
197 // FIXME: Start honoring ScaleBehavior to scale 2x buffers down to 1x. | 226 // FIXME: Start honoring ScaleBehavior to scale 2x buffers down to 1x. |
198 return BitmapImage::create(NativeImageSkia::create(copyBehavior == CopyBacki ngStore ? deepSkBitmapCopy(bitmap) : bitmap, m_resolutionScale)); | 227 return BitmapImage::create(NativeImageSkia::create(copyBehavior == CopyBacki ngStore ? deepSkBitmapCopy(bitmap) : bitmap, m_resolutionScale)); |
199 } | 228 } |
200 | 229 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
489 if (!encodeImage(imageData, mimeType, quality, &encodedImage)) | 518 if (!encodeImage(imageData, mimeType, quality, &encodedImage)) |
490 return "data:,"; | 519 return "data:,"; |
491 | 520 |
492 Vector<char> base64Data; | 521 Vector<char> base64Data; |
493 base64Encode(encodedImage, base64Data); | 522 base64Encode(encodedImage, base64Data); |
494 | 523 |
495 return "data:" + mimeType + ";base64," + base64Data; | 524 return "data:" + mimeType + ";base64," + base64Data; |
496 } | 525 } |
497 | 526 |
498 } // namespace WebCore | 527 } // namespace WebCore |
OLD | NEW |