Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / image / ImageFactory.cpp
blobfbe2cac41bbc8295b0ed1d9e48d2009c279eefab
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 "nsIChannel.h"
14 #include "nsIFileChannel.h"
15 #include "nsIObserverService.h"
16 #include "nsIFile.h"
17 #include "nsMimeTypes.h"
18 #include "nsIRequest.h"
20 #include "MultipartImage.h"
21 #include "RasterImage.h"
22 #include "VectorImage.h"
23 #include "Image.h"
24 #include "nsMediaFragmentURIParser.h"
25 #include "nsContentUtils.h"
27 #include "mozilla/SchedulerGroup.h"
28 #include "mozilla/StaticPrefs_image.h"
29 #include "mozilla/ProfilerMarkers.h"
31 namespace mozilla {
32 namespace image {
34 /*static*/
35 void ImageFactory::Initialize() {}
37 static uint32_t ComputeImageFlags(nsIURI* uri, const nsCString& aMimeType,
38 bool isMultiPart) {
39 // We default to the static globals.
40 bool isDiscardable = StaticPrefs::image_mem_discardable();
41 bool doDecodeImmediately = StaticPrefs::image_decode_immediately_enabled();
43 // We want UI to be as snappy as possible and not to flicker. Disable
44 // discarding for chrome URLS.
45 if (uri->SchemeIs("chrome")) {
46 isDiscardable = false;
49 // We don't want resources like the "loading" icon to be discardable either.
50 if (uri->SchemeIs("resource")) {
51 isDiscardable = false;
54 // For multipart/x-mixed-replace, we basically want a direct channel to the
55 // decoder. Disable everything for this case.
56 if (isMultiPart) {
57 isDiscardable = false;
60 // We have all the information we need.
61 uint32_t imageFlags = Image::INIT_FLAG_NONE;
62 if (isDiscardable) {
63 imageFlags |= Image::INIT_FLAG_DISCARDABLE;
65 if (doDecodeImmediately) {
66 imageFlags |= Image::INIT_FLAG_DECODE_IMMEDIATELY;
68 if (isMultiPart) {
69 imageFlags |= Image::INIT_FLAG_TRANSIENT;
72 // Synchronously decode metadata (including size) if we have a data URI since
73 // the data is immediately available.
74 if (uri->SchemeIs("data")) {
75 imageFlags |= Image::INIT_FLAG_SYNC_LOAD;
78 return imageFlags;
81 #ifdef DEBUG
82 static void NotifyImageLoading(nsIURI* aURI) {
83 if (!NS_IsMainThread()) {
84 nsCOMPtr<nsIURI> uri(aURI);
85 nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
86 "NotifyImageLoading", [uri]() -> void { NotifyImageLoading(uri); });
87 NS_DispatchToMainThread(ev.forget());
88 return;
91 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
92 NS_WARNING_ASSERTION(obs, "Can't get an observer service handle");
93 if (obs) {
94 nsAutoCString spec;
95 aURI->GetSpec(spec);
96 obs->NotifyObservers(nullptr, "image-loading",
97 NS_ConvertUTF8toUTF16(spec).get());
100 #endif
102 /* static */
103 already_AddRefed<Image> ImageFactory::CreateImage(
104 nsIRequest* aRequest, ProgressTracker* aProgressTracker,
105 const nsCString& aMimeType, nsIURI* aURI, bool aIsMultiPart,
106 uint64_t aInnerWindowId) {
107 // Compute the image's initialization flags.
108 uint32_t imageFlags = ComputeImageFlags(aURI, aMimeType, aIsMultiPart);
110 #ifdef DEBUG
111 // Record the image load for startup performance testing.
112 if (aURI->SchemeIs("resource") || aURI->SchemeIs("chrome")) {
113 NotifyImageLoading(aURI);
115 #endif
117 if (profiler_thread_is_being_profiled_for_markers()) {
118 static const size_t sMaxTruncatedLength = 1024;
119 PROFILER_MARKER_TEXT(
120 "Image Load", GRAPHICS, MarkerInnerWindowId(aInnerWindowId),
121 nsContentUtils::TruncatedURLForDisplay(aURI, sMaxTruncatedLength));
124 // Select the type of image to create based on MIME type.
125 if (aMimeType.EqualsLiteral(IMAGE_SVG_XML)) {
126 return CreateVectorImage(aRequest, aProgressTracker, aMimeType, aURI,
127 imageFlags, aInnerWindowId);
128 } else {
129 return CreateRasterImage(aRequest, aProgressTracker, aMimeType, aURI,
130 imageFlags, aInnerWindowId);
134 // Marks an image as having an error before returning it.
135 template <typename T>
136 static already_AddRefed<Image> BadImage(const char* aMessage,
137 RefPtr<T>& aImage) {
138 aImage->SetHasError();
139 return aImage.forget();
142 /* static */
143 already_AddRefed<Image> ImageFactory::CreateAnonymousImage(
144 const nsCString& aMimeType, uint32_t aSizeHint /* = 0 */) {
145 nsresult rv;
147 RefPtr<RasterImage> newImage = new RasterImage();
149 RefPtr<ProgressTracker> newTracker = new ProgressTracker();
150 newTracker->SetImage(newImage);
151 newImage->SetProgressTracker(newTracker);
153 rv = newImage->Init(aMimeType.get(), Image::INIT_FLAG_SYNC_LOAD);
154 if (NS_FAILED(rv)) {
155 return BadImage("RasterImage::Init failed", newImage);
158 rv = newImage->SetSourceSizeHint(aSizeHint);
159 if (NS_FAILED(rv)) {
160 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
163 return newImage.forget();
166 /* static */
167 already_AddRefed<MultipartImage> ImageFactory::CreateMultipartImage(
168 Image* aFirstPart, ProgressTracker* aProgressTracker) {
169 MOZ_ASSERT(aFirstPart);
170 MOZ_ASSERT(aProgressTracker);
172 RefPtr<MultipartImage> newImage = new MultipartImage(aFirstPart);
173 aProgressTracker->SetImage(newImage);
174 newImage->SetProgressTracker(aProgressTracker);
176 newImage->Init();
178 return newImage.forget();
181 int32_t SaturateToInt32(int64_t val) {
182 if (val > INT_MAX) {
183 return INT_MAX;
185 if (val < INT_MIN) {
186 return INT_MIN;
189 return static_cast<int32_t>(val);
192 uint32_t GetContentSize(nsIRequest* aRequest) {
193 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
194 if (channel) {
195 int64_t size;
196 nsresult rv = channel->GetContentLength(&size);
197 if (NS_SUCCEEDED(rv)) {
198 return std::max(SaturateToInt32(size), 0);
202 // Use the file size as a size hint for file channels.
203 nsCOMPtr<nsIFileChannel> fileChannel(do_QueryInterface(aRequest));
204 if (fileChannel) {
205 nsCOMPtr<nsIFile> file;
206 nsresult rv = fileChannel->GetFile(getter_AddRefs(file));
207 if (NS_SUCCEEDED(rv)) {
208 int64_t filesize;
209 rv = file->GetFileSize(&filesize);
210 if (NS_SUCCEEDED(rv)) {
211 return std::max(SaturateToInt32(filesize), 0);
216 // Fallback - neither http nor file. We'll use dynamic allocation.
217 return 0;
220 /* static */
221 already_AddRefed<Image> ImageFactory::CreateRasterImage(
222 nsIRequest* aRequest, ProgressTracker* aProgressTracker,
223 const nsCString& aMimeType, nsIURI* aURI, uint32_t aImageFlags,
224 uint64_t aInnerWindowId) {
225 MOZ_ASSERT(aProgressTracker);
227 nsresult rv;
229 RefPtr<RasterImage> newImage = new RasterImage(aURI);
230 aProgressTracker->SetImage(newImage);
231 newImage->SetProgressTracker(aProgressTracker);
233 rv = newImage->Init(aMimeType.get(), aImageFlags);
234 if (NS_FAILED(rv)) {
235 return BadImage("RasterImage::Init failed", newImage);
238 newImage->SetInnerWindowID(aInnerWindowId);
240 rv = newImage->SetSourceSizeHint(GetContentSize(aRequest));
241 if (NS_FAILED(rv)) {
242 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
245 return newImage.forget();
248 /* static */
249 already_AddRefed<Image> ImageFactory::CreateVectorImage(
250 nsIRequest* aRequest, ProgressTracker* aProgressTracker,
251 const nsCString& aMimeType, nsIURI* aURI, uint32_t aImageFlags,
252 uint64_t aInnerWindowId) {
253 MOZ_ASSERT(aProgressTracker);
255 nsresult rv;
257 RefPtr<VectorImage> newImage = new VectorImage(aURI);
258 aProgressTracker->SetImage(newImage);
259 newImage->SetProgressTracker(aProgressTracker);
261 rv = newImage->Init(aMimeType.get(), aImageFlags);
262 if (NS_FAILED(rv)) {
263 return BadImage("VectorImage::Init failed", newImage);
266 newImage->SetInnerWindowID(aInnerWindowId);
268 rv = newImage->OnStartRequest(aRequest);
269 if (NS_FAILED(rv)) {
270 return BadImage("VectorImage::OnStartRequest failed", newImage);
273 return newImage.forget();
276 } // namespace image
277 } // namespace mozilla