Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image_rewrite_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_REWRITE_FILTER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_REWRITE_FILTER_H_
21 
22 #include <map>
23 
24 #include "net/instaweb/rewriter/cached_result.pb.h"
42 #include "pagespeed/kernel/http/image_types.pb.h"
44 
45 namespace net_instaweb {
46 
49  INLINE_SUCCESS,
50  INLINE_UNSUPPORTED_DEVICE,
51  INLINE_NOT_CRITICAL,
52  INLINE_NO_DATA,
53  INLINE_TOO_LARGE,
54  INLINE_CACHE_SMALL_IMAGES_UNREWRITTEN,
57  INLINE_SHORTCUT,
58  INLINE_INTERNAL_ERROR,
59 };
60 
65  class Context;
66  public:
67  typedef std::map<GoogleString, AssociatedImageInfo> AssociatedImageInfoMap;
68 
70  static const char kImageNoRewritesHighResolution[];
71  static const char kImageOngoingRewrites[];
72  static const char kImageResizedUsingRenderedDimensions[];
73  static const char kImageRewriteLatencyFailedMs[];
74  static const char kImageRewriteLatencyOkMs[];
75  static const char kImageRewriteLatencyTotalMs[];
76  static const char kImageRewritesDroppedDecodeFailure[];
77  static const char kImageRewritesDroppedDueToLoad[];
78  static const char kImageRewritesDroppedMIMETypeUnknown[];
79  static const char kImageRewritesDroppedNoSavingNoResize[];
80  static const char kImageRewritesDroppedNoSavingResize[];
81  static const char kImageRewritesDroppedServerWriteFail[];
82  static const char kImageRewritesSquashingForMobileScreen[];
83  static const char kImageRewrites[];
84  static const char kImageWebpRewrites[];
85  static const char kImageWebpFromGifFailureMs[];
86  static const char kImageWebpFromGifSuccessMs[];
87  static const char kImageWebpFromGifTimeouts[];
88  static const char kImageWebpFromJpegFailureMs[];
89  static const char kImageWebpFromJpegSuccessMs[];
90  static const char kImageWebpFromJpegTimeouts[];
91  static const char kImageWebpFromPngFailureMs[];
92  static const char kImageWebpFromPngSuccessMs[];
93  static const char kImageWebpFromPngTimeouts[];
94  static const char kImageWebpOpaqueFailureMs[];
95  static const char kImageWebpOpaqueSuccessMs[];
96  static const char kImageWebpOpaqueTimeouts[];
97  static const char kImageWebpWithAlphaFailureMs[];
98  static const char kImageWebpWithAlphaSuccessMs[];
99  static const char kImageWebpWithAlphaTimeouts[];
100  static const char kImageWebpFromGifAnimatedFailureMs[];
101  static const char kImageWebpFromGifAnimatedSuccessMs[];
102  static const char kImageWebpFromGifAnimatedTimeouts[];
103 
107  static const char kInlinableImageUrlsPropertyName[];
108 
109  static const RewriteOptions::Filter kRelatedFilters[];
110  static const int kRelatedFiltersSize;
111 
112  explicit ImageRewriteFilter(RewriteDriver* driver);
113  virtual ~ImageRewriteFilter();
114  static void InitStats(Statistics* statistics);
115  static void Initialize();
116  static void Terminate();
117  static void AddRelatedOptions(StringPieceVector* target);
118  virtual void StartDocumentImpl();
119  virtual void EndDocument();
120  virtual void RenderDone();
121  virtual void StartElementImpl(HtmlElement* element);
122  virtual void EndElementImpl(HtmlElement* element) {}
123  virtual const char* Name() const { return "ImageRewrite"; }
124  virtual const char* id() const { return RewriteOptions::kImageCompressionId; }
126  ResourceContext* context) const;
127 
131  void RegisterImageInfo(const AssociatedImageInfo& image_info);
132 
138  static bool ExtractAssociatedImageInfo(const CachedResult* in,
139  RewriteContext* context,
140  AssociatedImageInfo* out);
141 
144  InlineResult TryInline(bool is_html, bool is_critical,
145  int64 image_inline_max_bytes, const CachedResult* cached_result,
146  ResourceSlot* slot, GoogleString* data_url);
147 
167  static bool ParseDimensionAttribute(const char* position, int* value);
168 
172  RewriteContext* MakeNestedRewriteContextForCss(
173  int64 css_image_inline_max_bytes,
174  RewriteContext* parent,
175  const ResourceSlotPtr& slot);
176 
179  virtual RewriteContext* MakeNestedRewriteContext(RewriteContext* parent,
180  const ResourceSlotPtr& slot);
181 
185  const ImageDim& image_dim, const ResourceContext& resource_context,
186  ImageDim* desired_dim);
187 
191  bool ShouldResize(const ResourceContext& context,
192  const GoogleString& url,
193  Image* image,
194  ImageDim* desired_dimensions);
195 
199  const Context* rewrite_context, const GoogleString& url,
200  ResourceContext* context, Image* image, CachedResult* cached);
201 
204  Image::CompressionOptions* ImageOptionsForLoadedResource(
205  const ResourceContext& context, const ResourcePtr& input_resource);
206 
207  virtual const RewriteOptions::Filter* RelatedFilters(int* num_filters) const;
208  virtual const StringPieceVector* RelatedOptions() const {
209  return related_options_;
210  }
211 
213  static void DisableRelatedFilters(RewriteOptions* options);
214 
216  void ReportDroppedRewrite();
217 
218  protected:
219  virtual const UrlSegmentEncoder* encoder() const;
220 
222 
223  private:
224  friend class Context;
225 
227  void InfoAndTrace(Context* context, const char* format, ...)
229  const ContentType* ImageToContentType(const GoogleString& origin_url,
230  Image* image);
231  void BeginRewriteImageUrl(HtmlElement* element, HtmlElement::Attribute* src);
232  void BeginRewriteSrcSet(HtmlElement* element, HtmlElement::Attribute* srcset);
233 
234  void ComputePreserveUrls(const RewriteOptions* options, ResourceSlot* slot);
235 
236  RewriteResult RewriteLoadedResourceImpl(Context* context,
237  const ResourcePtr& input_resource,
238  const OutputResourcePtr& result);
239 
241  bool FinishRewriteCssImageUrl(
242  int64 css_image_inline_max_bytes,
243  const CachedResult* cached, ResourceSlot* slot,
244  InlineResult* inline_result);
245 
247  bool FinishRewriteImageUrl(
248  const CachedResult* cached, const ResourceContext* resource_context,
249  HtmlElement* element, HtmlElement::Attribute* src, int image_index,
250  HtmlResourceSlot* slot, InlineResult* inline_result);
251 
253  void SaveIfInlinable(const StringPiece& contents,
254  const ImageType image_type,
255  CachedResult* cached);
256 
261  void GetDimensions(HtmlElement* element, ImageDim* page_dim,
262  const HtmlElement::Attribute* src,
263  bool* is_resized_using_rendered_dimensions);
264 
267  bool HasAnyDimensions(HtmlElement* element);
268 
271  void ResizeLowQualityImage(
272  Image* low_image, const ResourcePtr& input_resource,
273  CachedResult* cached);
274 
279  bool IsHtmlCriticalImage(StringPiece image_url) const;
280 
284  bool StoreUrlInPropertyCache(const StringPiece& url);
285 
286  void SaveDebugMessageToCache(const GoogleString& message,
287  CachedResult* cached_result);
288 
289  GoogleString UrlForDebugMessages(const Context* context) const;
290 
292 
294  Variable* image_rewrites_;
296  Variable* image_resized_using_rendered_dimensions_;
298  Variable* image_norewrites_high_resolution_;
303  Variable* image_rewrites_dropped_intentionally_;
305  Variable* image_rewrites_dropped_decode_failure_;
307  Variable* image_rewrites_dropped_mime_type_unknown_;
310  Variable* image_rewrites_dropped_server_write_fail_;
313  Variable* image_rewrites_dropped_nosaving_resize_;
316  Variable* image_rewrites_dropped_nosaving_noresize_;
318  TimedVariable* image_rewrites_dropped_due_to_load_;
322  TimedVariable* image_rewrites_squashing_for_mobile_screen_;
326  Variable* image_rewrite_total_bytes_saved_;
330  Variable* image_rewrite_total_original_bytes_;
333  Variable* image_rewrite_uses_;
335  Variable* image_inline_count_;
337  Variable* image_webp_rewrites_;
339  UpDownCounter* image_ongoing_rewrites_;
340 
342  Variable* image_rewrite_latency_total_ms_;
344  Histogram* image_rewrite_latency_ok_ms_;
346  Histogram* image_rewrite_latency_failed_ms_;
347 
348  ImageUrlEncoder encoder_;
349 
352  int image_counter_;
353 
357  StringSet inlinable_urls_;
358 
360  Image::ConversionVariables webp_conversion_variables_;
361 
363  static StringPieceVector* related_options_;
364 
365  std::map<GoogleString, AssociatedImageInfo> image_info_;
367  bool saw_end_document_;
368 
369 
370 };
371 
372 }
373 
374 #endif
InlineResult
See MessageForInlineResult for enum meanings.
Definition: image_rewrite_filter.h:48
void ReportDroppedRewrite()
Update stats for a rewrite dropped due to load.
RewriteContext * MakeNestedRewriteContextForCss(int64 css_image_inline_max_bytes, RewriteContext *parent, const ResourceSlotPtr &slot)
bool ResizeImageIfNecessary(const Context *rewrite_context, const GoogleString &url, ResourceContext *context, Image *image, CachedResult *cached)
Definition: statistics.h:43
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: image_rewrite_filter.h:123
virtual RewriteContext * MakeRewriteContext()
virtual void EncodeUserAgentIntoResourceContext(ResourceContext *context) const
InlineResult TryInline(bool is_html, bool is_critical, int64 image_inline_max_bytes, const CachedResult *cached_result, ResourceSlot *slot, GoogleString *data_url)
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Definition: resource_slot.h:54
Definition: html_element.h:42
bool UpdateDesiredImageDimsIfNecessary(const ImageDim &image_dim, const ResourceContext &resource_context, ImageDim *desired_dim)
static bool ParseDimensionAttribute(const char *position, int *value)
DimensionState GetDimensions(Css::Declarations *decls, int *width, int *height)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: resource_slot.h:259
Image::CompressionOptions * ImageOptionsForLoadedResource(const ResourceContext &context, const ResourcePtr &input_resource)
static void DisableRelatedFilters(RewriteOptions *options)
Disable all filters listed in kRelatedFilters in options.
virtual RewriteContext * MakeNestedRewriteContext(RewriteContext *parent, const ResourceSlotPtr &slot)
Definition: rewrite_filter.h:35
virtual const StringPieceVector * RelatedOptions() const
Definition: image_rewrite_filter.h:208
Filter
Definition: rewrite_options.h:106
Definition: rewrite_driver.h:100
virtual const UrlSegmentEncoder * encoder() const
Image should not be inlined because it is part of a responsive image.
Definition: image_rewrite_filter.h:56
Definition: content_type.h:31
RewriteResult
Definition: rewrite_result.h:27
void RegisterImageInfo(const AssociatedImageInfo &image_info)
virtual const RewriteOptions::Filter * RelatedFilters(int *num_filters) const
Definition: rewrite_context.h:155
static const char kInlinableImageUrlsPropertyName[]
Definition: image_rewrite_filter.h:107
Definition: image_rewrite_filter.h:64
Definition: statistics.h:138
Definition: url_segment_encoder.h:33
Definition: statistics.h:73
#define INSTAWEB_PRINTF_FORMAT(x, y)
< Not GCC
Definition: printf_format.h:34
Definition: image.h:39
Definition: statistics.h:298
static bool ExtractAssociatedImageInfo(const CachedResult *in, RewriteContext *context, AssociatedImageInfo *out)
static const char kImageNoRewritesHighResolution[]
Statistic names:
Definition: image_rewrite_filter.h:70
Definition: rewrite_options.h:84
Definition: image_url_encoder.h:62
bool ShouldResize(const ResourceContext &context, const GoogleString &url, Image *image, ImageDim *desired_dimensions)