Bug 1619273 [wpt PR 22036] - Don't compare px rounded font sizes., a=testonly
[gecko.git] / image / ImageFactory.cpp
blobe26e472c949afa8023a768ca27d065229f66affc
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ImageFactory.h"
9 #include <algorithm>
11 #include "mozilla/Likely.h"
13 #include "nsIFileChannel.h"
14 #include "nsIFile.h"
15 #include "nsMimeTypes.h"
16 #include "nsIRequest.h"
18 #include "MultipartImage.h"
19 #include "RasterImage.h"
20 #include "VectorImage.h"
21 #include "Image.h"
22 #include "nsMediaFragmentURIParser.h"
23 #include "nsContentUtils.h"
25 #include "mozilla/StaticPrefs_image.h"
27 namespace mozilla {
28 namespace image {
30 /*static*/
31 void ImageFactory::Initialize() {}
33 static uint32_t ComputeImageFlags(nsIURI* uri, const nsCString& aMimeType,
34 bool isMultiPart) {
35 // We default to the static globals.
36 bool isDiscardable = StaticPrefs::image_mem_discardable();
37 bool doDecodeImmediately = StaticPrefs::image_decode_immediately_enabled();
39 // We want UI to be as snappy as possible and not to flicker. Disable
40 // discarding for chrome URLS.
41 if (uri->SchemeIs("chrome")) {
42 isDiscardable = false;
45 // We don't want resources like the "loading" icon to be discardable either.
46 if (uri->SchemeIs("resource")) {
47 isDiscardable = false;
50 // For multipart/x-mixed-replace, we basically want a direct channel to the
51 // decoder. Disable everything for this case.
52 if (isMultiPart) {
53 isDiscardable = false;
56 // We have all the information we need.
57 uint32_t imageFlags = Image::INIT_FLAG_NONE;
58 if (isDiscardable) {
59 imageFlags |= Image::INIT_FLAG_DISCARDABLE;
61 if (doDecodeImmediately) {
62 imageFlags |= Image::INIT_FLAG_DECODE_IMMEDIATELY;
64 if (isMultiPart) {
65 imageFlags |= Image::INIT_FLAG_TRANSIENT;
68 // Synchronously decode metadata (including size) if we have a data URI since
69 // the data is immediately available.
70 if (uri->SchemeIs("data")) {
71 imageFlags |= Image::INIT_FLAG_SYNC_LOAD;
74 return imageFlags;
77 #ifdef DEBUG
78 static void NotifyImageLoading(nsIURI* aURI) {
79 if (!NS_IsMainThread()) {
80 nsCOMPtr<nsIURI> uri(aURI);
81 nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
82 "NotifyImageLoading", [uri]() -> void { NotifyImageLoading(uri); });
83 SystemGroup::Dispatch(TaskCategory::Other, ev.forget());
84 return;
87 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
88 NS_WARNING_ASSERTION(obs, "Can't get an observer service handle");
89 if (obs) {
90 nsAutoCString spec;
91 aURI->GetSpec(spec);
92 obs->NotifyObservers(nullptr, "image-loading",
93 NS_ConvertUTF8toUTF16(spec).get());
96 #endif
98 /* static */
99 already_AddRefed<Image> ImageFactory::CreateImage(
100 nsIRequest* aRequest, ProgressTracker* aProgressTracker,
101 const nsCString& aMimeType, nsIURI* aURI, bool aIsMultiPart,
102 uint32_t aInnerWindowId) {
103 // Compute the image's initialization flags.
104 uint32_t imageFlags = ComputeImageFlags(aURI, aMimeType, aIsMultiPart);
106 #ifdef DEBUG
107 // Record the image load for startup performance testing.
108 if (aURI->SchemeIs("resource") || aURI->SchemeIs("chrome")) {
109 NotifyImageLoading(aURI);
111 #endif
113 // Select the type of image to create based on MIME type.
114 if (aMimeType.EqualsLiteral(IMAGE_SVG_XML)) {
115 return CreateVectorImage(aRequest, aProgressTracker, aMimeType, aURI,
116 imageFlags, aInnerWindowId);
117 } else {
118 return CreateRasterImage(aRequest, aProgressTracker, aMimeType, aURI,
119 imageFlags, aInnerWindowId);
123 // Marks an image as having an error before returning it.
124 template <typename T>
125 static already_AddRefed<Image> BadImage(const char* aMessage,
126 RefPtr<T>& aImage) {
127 aImage->SetHasError();
128 return aImage.forget();
131 /* static */
132 already_AddRefed<Image> ImageFactory::CreateAnonymousImage(
133 const nsCString& aMimeType, uint32_t aSizeHint /* = 0 */) {
134 nsresult rv;
136 RefPtr<RasterImage> newImage = new RasterImage();
138 RefPtr<ProgressTracker> newTracker = new ProgressTracker();
139 newTracker->SetImage(newImage);
140 newImage->SetProgressTracker(newTracker);
142 rv = newImage->Init(aMimeType.get(), Image::INIT_FLAG_SYNC_LOAD);
143 if (NS_FAILED(rv)) {
144 return BadImage("RasterImage::Init failed", newImage);
147 rv = newImage->SetSourceSizeHint(aSizeHint);
148 if (NS_FAILED(rv)) {
149 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
152 return newImage.forget();
155 /* static */
156 already_AddRefed<MultipartImage> ImageFactory::CreateMultipartImage(
157 Image* aFirstPart, ProgressTracker* aProgressTracker) {
158 MOZ_ASSERT(aFirstPart);
159 MOZ_ASSERT(aProgressTracker);
161 RefPtr<MultipartImage> newImage = new MultipartImage(aFirstPart);
162 aProgressTracker->SetImage(newImage);
163 newImage->SetProgressTracker(aProgressTracker);
165 newImage->Init();
167 return newImage.forget();
170 int32_t SaturateToInt32(int64_t val) {
171 if (val > INT_MAX) {
172 return INT_MAX;
174 if (val < INT_MIN) {
175 return INT_MIN;
178 return static_cast<int32_t>(val);
181 uint32_t GetContentSize(nsIRequest* aRequest) {
182 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
183 if (channel) {
184 int64_t size;
185 nsresult rv = channel->GetContentLength(&size);
186 if (NS_SUCCEEDED(rv)) {
187 return std::max(SaturateToInt32(size), 0);
191 // Use the file size as a size hint for file channels.
192 nsCOMPtr<nsIFileChannel> fileChannel(do_QueryInterface(aRequest));
193 if (fileChannel) {
194 nsCOMPtr<nsIFile> file;
195 nsresult rv = fileChannel->GetFile(getter_AddRefs(file));
196 if (NS_SUCCEEDED(rv)) {
197 int64_t filesize;
198 rv = file->GetFileSize(&filesize);
199 if (NS_SUCCEEDED(rv)) {
200 return std::max(SaturateToInt32(filesize), 0);
205 // Fallback - neither http nor file. We'll use dynamic allocation.
206 return 0;
209 /* static */
210 already_AddRefed<Image> ImageFactory::CreateRasterImage(
211 nsIRequest* aRequest, ProgressTracker* aProgressTracker,
212 const nsCString& aMimeType, nsIURI* aURI, uint32_t aImageFlags,
213 uint32_t aInnerWindowId) {
214 MOZ_ASSERT(aProgressTracker);
216 nsresult rv;
218 RefPtr<RasterImage> newImage = new RasterImage(aURI);
219 aProgressTracker->SetImage(newImage);
220 newImage->SetProgressTracker(aProgressTracker);
222 rv = newImage->Init(aMimeType.get(), aImageFlags);
223 if (NS_FAILED(rv)) {
224 return BadImage("RasterImage::Init failed", newImage);
227 newImage->SetInnerWindowID(aInnerWindowId);
229 rv = newImage->SetSourceSizeHint(GetContentSize(aRequest));
230 if (NS_FAILED(rv)) {
231 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
234 return newImage.forget();
237 /* static */
238 already_AddRefed<Image> ImageFactory::CreateVectorImage(
239 nsIRequest* aRequest, ProgressTracker* aProgressTracker,
240 const nsCString& aMimeType, nsIURI* aURI, uint32_t aImageFlags,
241 uint32_t aInnerWindowId) {
242 MOZ_ASSERT(aProgressTracker);
244 nsresult rv;
246 RefPtr<VectorImage> newImage = new VectorImage(aURI);
247 aProgressTracker->SetImage(newImage);
248 newImage->SetProgressTracker(aProgressTracker);
250 rv = newImage->Init(aMimeType.get(), aImageFlags);
251 if (NS_FAILED(rv)) {
252 return BadImage("VectorImage::Init failed", newImage);
255 newImage->SetInnerWindowID(aInnerWindowId);
257 rv = newImage->OnStartRequest(aRequest);
258 if (NS_FAILED(rv)) {
259 return BadImage("VectorImage::OnStartRequest failed", newImage);
262 return newImage.forget();
265 } // namespace image
266 } // namespace mozilla