Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: third_party/WebKit/Source/core/imagebitmap/ImageBitmapFactories.cpp

Issue 2035113002: Implement ImageBitmapOptions resize (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 if (value.isBlob()) 79 if (value.isBlob())
80 return value.getAsBlob(); 80 return value.getAsBlob();
81 if (value.isImageData()) 81 if (value.isImageData())
82 return value.getAsImageData(); 82 return value.getAsImageData();
83 if (value.isImageBitmap()) 83 if (value.isImageBitmap())
84 return value.getAsImageBitmap(); 84 return value.getAsImageBitmap();
85 ASSERT_NOT_REACHED(); 85 ASSERT_NOT_REACHED();
86 return nullptr; 86 return nullptr;
87 } 87 }
88 88
89 ScriptPromise ImageBitmapFactories::createImageBitmapFromBlob(ScriptState* scrip tState, EventTarget& eventTarget, ImageBitmapSource* bitmapSource, const IntRect & cropRect, const ImageBitmapOptions& options) 89 ScriptPromise ImageBitmapFactories::createImageBitmapFromBlob(ScriptState* scrip tState, EventTarget& eventTarget, ImageBitmapSource* bitmapSource, Optional<IntR ect> cropRect, const ImageBitmapOptions& options, ExceptionState& exceptionState )
90 { 90 {
91 if (cropRect && !ImageBitmap::isSourceSizeValid(cropRect->width(), cropRect- >height(), exceptionState))
92 return ScriptPromise();
93 if (!ImageBitmap::isResizeOptionValid(options, exceptionState))
94 return ScriptPromise();
91 Blob* blob = static_cast<Blob*>(bitmapSource); 95 Blob* blob = static_cast<Blob*>(bitmapSource);
92 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create( from(eventTarget), cropRect, options, scriptState); 96 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create( from(eventTarget), cropRect, options, scriptState);
93 ScriptPromise promise = loader->promise(); 97 ScriptPromise promise = loader->promise();
94 from(eventTarget).addLoader(loader); 98 from(eventTarget).addLoader(loader);
95 loader->loadBlobAsync(eventTarget.getExecutionContext(), blob); 99 loader->loadBlobAsync(eventTarget.getExecutionContext(), blob);
96 return promise; 100 return promise;
97 } 101 }
98 102
99 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, const Imag eBitmapOptions& options, ExceptionState& exceptionState) 103 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, const Imag eBitmapOptions& options, ExceptionState& exceptionState)
100 { 104 {
101 UseCounter::Feature feature = UseCounter::CreateImageBitmap; 105 UseCounter::Feature feature = UseCounter::CreateImageBitmap;
102 UseCounter::count(scriptState->getExecutionContext(), feature); 106 UseCounter::count(scriptState->getExecutionContext(), feature);
103 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap Source, exceptionState, false); 107 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap Source, exceptionState, false);
104 if (!bitmapSourceInternal) 108 if (!bitmapSourceInternal)
105 return ScriptPromise(); 109 return ScriptPromise();
106 if (bitmapSourceInternal->isBlob()) 110 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, Opt ional<IntRect>(), options, exceptionState);
107 return createImageBitmapFromBlob(scriptState, eventTarget, bitmapSourceI nternal, IntRect(), options);
108 IntSize srcSize = bitmapSourceInternal->bitmapSourceSize();
109 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, 0, 0, srcSize.width(), srcSize.height(), options, exceptionState);
110 } 111 }
111 112
112 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, int sx, in t sy, int sw, int sh, const ImageBitmapOptions& options, ExceptionState& excepti onState) 113 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, int sx, in t sy, int sw, int sh, const ImageBitmapOptions& options, ExceptionState& excepti onState)
113 { 114 {
114 UseCounter::Feature feature = UseCounter::CreateImageBitmap; 115 UseCounter::Feature feature = UseCounter::CreateImageBitmap;
115 UseCounter::count(scriptState->getExecutionContext(), feature); 116 UseCounter::count(scriptState->getExecutionContext(), feature);
116 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap Source, exceptionState, true); 117 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap Source, exceptionState, true);
117 if (!bitmapSourceInternal) 118 if (!bitmapSourceInternal)
118 return ScriptPromise(); 119 return ScriptPromise();
119 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, sx, sy, sw, sh, options, exceptionState); 120 Optional<IntRect> cropRect = IntRect(sx, sy, sw, sh);
121 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, cro pRect, options, exceptionState);
120 } 122 }
121 123
122 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmapSource* bitmapSource, int sx, int sy, int s w, int sh, const ImageBitmapOptions& options, ExceptionState& exceptionState) 124 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmapSource* bitmapSource, Optional<IntRect> cro pRect, const ImageBitmapOptions& options, ExceptionState& exceptionState)
123 { 125 {
124 if (bitmapSource->isBlob()) { 126 if (bitmapSource->isBlob())
125 if (!sw || !sh) { 127 return createImageBitmapFromBlob(scriptState, eventTarget, bitmapSource, cropRect, options, exceptionState);
126 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
127 return ScriptPromise();
128 }
129 return createImageBitmapFromBlob(scriptState, eventTarget, bitmapSource, IntRect(sx, sy, sw, sh), options);
130 }
131 128
132 return bitmapSource->createImageBitmap(scriptState, eventTarget, sx, sy, sw, sh, options, exceptionState); 129 return bitmapSource->createImageBitmap(scriptState, eventTarget, cropRect, o ptions, exceptionState);
133 } 130 }
134 131
135 const char* ImageBitmapFactories::supplementName() 132 const char* ImageBitmapFactories::supplementName()
136 { 133 {
137 return "ImageBitmapFactories"; 134 return "ImageBitmapFactories";
138 } 135 }
139 136
140 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget) 137 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget)
141 { 138 {
142 if (LocalDOMWindow* window = eventTarget.toLocalDOMWindow()) 139 if (LocalDOMWindow* window = eventTarget.toLocalDOMWindow())
(...skipping 18 matching lines...) Expand all
161 { 158 {
162 m_pendingLoaders.add(loader); 159 m_pendingLoaders.add(loader);
163 } 160 }
164 161
165 void ImageBitmapFactories::didFinishLoading(ImageBitmapLoader* loader) 162 void ImageBitmapFactories::didFinishLoading(ImageBitmapLoader* loader)
166 { 163 {
167 ASSERT(m_pendingLoaders.contains(loader)); 164 ASSERT(m_pendingLoaders.contains(loader));
168 m_pendingLoaders.remove(loader); 165 m_pendingLoaders.remove(loader);
169 } 166 }
170 167
171 ImageBitmapFactories::ImageBitmapLoader::ImageBitmapLoader(ImageBitmapFactories& factory, const IntRect& cropRect, ScriptState* scriptState, const ImageBitmapOp tions& options) 168 ImageBitmapFactories::ImageBitmapLoader::ImageBitmapLoader(ImageBitmapFactories& factory, Optional<IntRect> cropRect, ScriptState* scriptState, const ImageBitma pOptions& options)
172 : m_loader(FileReaderLoader::ReadAsArrayBuffer, this) 169 : m_loader(FileReaderLoader::ReadAsArrayBuffer, this)
173 , m_factory(&factory) 170 , m_factory(&factory)
174 , m_resolver(ScriptPromiseResolver::create(scriptState)) 171 , m_resolver(ScriptPromiseResolver::create(scriptState))
175 , m_cropRect(cropRect) 172 , m_cropRect(cropRect)
176 , m_options(options) 173 , m_options(options)
177 { 174 {
178 } 175 }
179 176
180 void ImageBitmapFactories::ImageBitmapLoader::loadBlobAsync(ExecutionContext* co ntext, Blob* blob) 177 void ImageBitmapFactories::ImageBitmapLoader::loadBlobAsync(ExecutionContext* co ntext, Blob* blob)
181 { 178 {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 void ImageBitmapFactories::ImageBitmapLoader::resolvePromiseOnOriginalThread(Pas sRefPtr<SkImage> frame) 243 void ImageBitmapFactories::ImageBitmapLoader::resolvePromiseOnOriginalThread(Pas sRefPtr<SkImage> frame)
247 { 244 {
248 if (!frame) { 245 if (!frame) {
249 rejectPromise(); 246 rejectPromise();
250 return; 247 return;
251 } 248 }
252 ASSERT(frame->width() && frame->height()); 249 ASSERT(frame->width() && frame->height());
253 250
254 RefPtr<StaticBitmapImage> image = StaticBitmapImage::create(frame); 251 RefPtr<StaticBitmapImage> image = StaticBitmapImage::create(frame);
255 image->setOriginClean(true); 252 image->setOriginClean(true);
256 if (!m_cropRect.width() && !m_cropRect.height()) {
257 // No cropping variant was called.
258 m_cropRect = IntRect(IntPoint(), image->size());
259 }
260 253
261 ImageBitmap* imageBitmap = ImageBitmap::create(image, m_cropRect, m_options) ; 254 ImageBitmap* imageBitmap = ImageBitmap::create(image, m_cropRect, m_options) ;
262 if (imageBitmap && imageBitmap->bitmapImage()) { 255 if (imageBitmap && imageBitmap->bitmapImage()) {
263 m_resolver->resolve(imageBitmap); 256 m_resolver->resolve(imageBitmap);
264 } else { 257 } else {
265 rejectPromise(); 258 rejectPromise();
266 return; 259 return;
267 } 260 }
268 m_factory->didFinishLoading(this); 261 m_factory->didFinishLoading(this);
269 } 262 }
270 263
271 DEFINE_TRACE(ImageBitmapFactories::ImageBitmapLoader) 264 DEFINE_TRACE(ImageBitmapFactories::ImageBitmapLoader)
272 { 265 {
273 visitor->trace(m_factory); 266 visitor->trace(m_factory);
274 visitor->trace(m_resolver); 267 visitor->trace(m_resolver);
275 } 268 }
276 269
277 } // namespace blink 270 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698