| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "core/imagebitmap/ImageBitmapFactories.h" | 32 #include "core/imagebitmap/ImageBitmapFactories.h" |
| 33 | 33 |
| 34 #include "bindings/core/v8/ExceptionState.h" | 34 #include "bindings/core/v8/ExceptionState.h" |
| 35 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 36 #include "core/dom/ExecutionContext.h" | 35 #include "core/dom/ExecutionContext.h" |
| 37 #include "core/fileapi/Blob.h" | 36 #include "core/fileapi/Blob.h" |
| 38 #include "core/frame/ImageBitmap.h" | 37 #include "core/frame/ImageBitmap.h" |
| 39 #include "core/frame/LocalDOMWindow.h" | 38 #include "core/frame/LocalDOMWindow.h" |
| 39 #include "core/html/HTMLCanvasElement.h" |
| 40 #include "core/html/HTMLImageElement.h" |
| 41 #include "core/html/HTMLVideoElement.h" |
| 40 #include "core/html/ImageData.h" | 42 #include "core/html/ImageData.h" |
| 41 #include "core/workers/WorkerGlobalScope.h" | 43 #include "core/workers/WorkerGlobalScope.h" |
| 42 #include "platform/SharedBuffer.h" | 44 #include "platform/SharedBuffer.h" |
| 43 #include "platform/graphics/ImageSource.h" | 45 #include "platform/graphics/ImageSource.h" |
| 44 #include "platform/graphics/StaticBitmapImage.h" | |
| 45 #include "public/platform/WebSize.h" | |
| 46 #include "third_party/skia/include/core/SkImage.h" | |
| 47 #include <v8.h> | 46 #include <v8.h> |
| 48 | 47 |
| 49 namespace blink { | 48 namespace blink { |
| 50 | 49 |
| 51 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWill
BeRawPtr<ImageBitmap> imageBitmap) | 50 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWill
BeRawPtr<ImageBitmap> imageBitmap) |
| 52 { | 51 { |
| 53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 52 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 54 ScriptPromise promise = resolver->promise(); | 53 ScriptPromise promise = resolver->promise(); |
| 55 if (imageBitmap) { | 54 if (imageBitmap) { |
| 56 resolver->resolve(imageBitmap); | 55 resolver->resolve(imageBitmap); |
| 57 } else { | 56 } else { |
| 58 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate(
)))); | 57 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate(
)))); |
| 59 } | 58 } |
| 60 return promise; | 59 return promise; |
| 61 } | 60 } |
| 62 | 61 |
| 63 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, Blob* blob, ExceptionState& exceptionState) | 62 static inline ImageBitmapSource* toImageBitmapSourceInternal(const ImageBitmapSo
urceUnion& value) |
| 64 { | 63 { |
| 65 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create(
from(eventTarget), IntRect(), scriptState); | 64 if (value.isHTMLImageElement()) |
| 66 ScriptPromise promise = loader->promise(); | 65 return value.getAsHTMLImageElement().get(); |
| 67 from(eventTarget).addLoader(loader); | 66 if (value.isHTMLVideoElement()) |
| 68 loader->loadBlobAsync(eventTarget.executionContext(), blob); | 67 return value.getAsHTMLVideoElement().get(); |
| 69 return promise; | 68 if (value.isHTMLCanvasElement()) |
| 69 return value.getAsHTMLCanvasElement().get(); |
| 70 if (value.isBlob()) |
| 71 return value.getAsBlob(); |
| 72 if (value.isImageData()) |
| 73 return value.getAsImageData(); |
| 74 if (value.isImageBitmap()) |
| 75 return value.getAsImageBitmap().get(); |
| 76 ASSERT_NOT_REACHED(); |
| 77 return nullptr; |
| 70 } | 78 } |
| 71 | 79 |
| 72 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, Blob* blob, int sx, int sy, int sw, int sh, ExceptionS
tate& exceptionState) | 80 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, ExceptionS
tate& exceptionState) |
| 73 { | 81 { |
| 74 if (!sw || !sh) { | 82 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap
Source); |
| 75 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | 83 if (bitmapSourceInternal->isBlob()) { |
| 76 return ScriptPromise(); | 84 Blob* blob = static_cast<Blob*>(bitmapSourceInternal); |
| 85 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::cre
ate(from(eventTarget), IntRect(), scriptState); |
| 86 ScriptPromise promise = loader->promise(); |
| 87 from(eventTarget).addLoader(loader); |
| 88 loader->loadBlobAsync(eventTarget.executionContext(), blob); |
| 89 return promise; |
| 77 } | 90 } |
| 78 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create(
from(eventTarget), IntRect(sx, sy, sw, sh), scriptState); | 91 IntSize srcSize = bitmapSourceInternal->bitmapSourceSize(); |
| 79 ScriptPromise promise = loader->promise(); | 92 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, 0,
0, srcSize.width(), srcSize.height(), exceptionState); |
| 80 from(eventTarget).addLoader(loader); | |
| 81 loader->loadBlobAsync(eventTarget.executionContext(), blob); | |
| 82 return promise; | |
| 83 } | 93 } |
| 84 | 94 |
| 85 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, ImageData* data, ExceptionState& exceptionState) | 95 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, int sx, in
t sy, int sw, int sh, ExceptionState& exceptionState) |
| 86 { | 96 { |
| 87 return createImageBitmap(scriptState, eventTarget, data, 0, 0, data->width()
, data->height(), exceptionState); | 97 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap
Source); |
| 98 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, sx,
sy, sw, sh, exceptionState); |
| 88 } | 99 } |
| 89 | 100 |
| 90 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, ImageData* data, int sx, int sy, int sw, int sh, Excep
tionState& exceptionState) | 101 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, ImageBitmapSource* bitmapSource, int sx, int sy, int s
w, int sh, ExceptionState& exceptionState) |
| 91 { | 102 { |
| 92 if (!sw || !sh) { | 103 if (bitmapSource->isBlob()) { |
| 93 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | 104 if (!sw || !sh) { |
| 105 exceptionState.throwDOMException(IndexSizeError, String::format("The
source %s provided is 0.", sw ? "height" : "width")); |
| 106 return ScriptPromise(); |
| 107 } |
| 108 Blob* blob = static_cast<Blob*>(bitmapSource); |
| 109 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::cre
ate(from(eventTarget), IntRect(sx, sy, sw, sh), scriptState); |
| 110 ScriptPromise promise = loader->promise(); |
| 111 from(eventTarget).addLoader(loader); |
| 112 loader->loadBlobAsync(eventTarget.executionContext(), blob); |
| 113 return promise; |
| 114 } |
| 115 |
| 116 RefPtrWillBeRawPtr<ImageBitmap> imageBitmap = bitmapSource->createImageBitma
p(eventTarget, bitmapSource, sx, sy, sw, sh, exceptionState); |
| 117 if (!imageBitmap) |
| 94 return ScriptPromise(); | 118 return ScriptPromise(); |
| 95 } | 119 return fulfillImageBitmap(scriptState, imageBitmap); |
| 96 if (data->data()->bufferBase()->isNeutered()) { | |
| 97 exceptionState.throwDOMException(InvalidStateError, "The source data has
been neutered."); | |
| 98 return ScriptPromise(); | |
| 99 } | |
| 100 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
| 101 return fulfillImageBitmap(scriptState, ImageBitmap::create(data, IntRect(sx,
sy, sw, sh))); | |
| 102 } | |
| 103 | |
| 104 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, ImageBitmap* bitmap, ExceptionState& exceptionState) | |
| 105 { | |
| 106 return createImageBitmap(scriptState, eventTarget, bitmap, 0, 0, bitmap->wid
th(), bitmap->height(), exceptionState); | |
| 107 } | |
| 108 | |
| 109 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState,
EventTarget& eventTarget, ImageBitmap* bitmap, int sx, int sy, int sw, int sh, E
xceptionState& exceptionState) | |
| 110 { | |
| 111 if (!sw || !sh) { | |
| 112 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
| 113 return ScriptPromise(); | |
| 114 } | |
| 115 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
| 116 return fulfillImageBitmap(scriptState, ImageBitmap::create(bitmap, IntRect(s
x, sy, sw, sh))); | |
| 117 } | 120 } |
| 118 | 121 |
| 119 const char* ImageBitmapFactories::supplementName() | 122 const char* ImageBitmapFactories::supplementName() |
| 120 { | 123 { |
| 121 return "ImageBitmapFactories"; | 124 return "ImageBitmapFactories"; |
| 122 } | 125 } |
| 123 | 126 |
| 124 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget) | 127 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget) |
| 125 { | 128 { |
| 126 if (LocalDOMWindow* window = eventTarget.toDOMWindow()) | 129 if (LocalDOMWindow* window = eventTarget.toDOMWindow()) |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 rejectPromise(); | 215 rejectPromise(); |
| 213 } | 216 } |
| 214 | 217 |
| 215 DEFINE_TRACE(ImageBitmapFactories::ImageBitmapLoader) | 218 DEFINE_TRACE(ImageBitmapFactories::ImageBitmapLoader) |
| 216 { | 219 { |
| 217 visitor->trace(m_factory); | 220 visitor->trace(m_factory); |
| 218 visitor->trace(m_resolver); | 221 visitor->trace(m_resolver); |
| 219 } | 222 } |
| 220 | 223 |
| 221 } // namespace blink | 224 } // namespace blink |
| OLD | NEW |