Home | History | Annotate | Download | only in image
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_GFX_IMAGE_IMAGE_SKIA_REP_H_
      6 #define UI_GFX_IMAGE_IMAGE_SKIA_REP_H_
      7 
      8 #include "third_party/skia/include/core/SkBitmap.h"
      9 #include "ui/gfx/gfx_export.h"
     10 #include "ui/gfx/size.h"
     11 
     12 namespace gfx {
     13 
     14 // An ImageSkiaRep represents a bitmap and the scale factor it is intended for.
     15 // 0.0f scale is used to indicate that this ImageSkiaRep is used for unscaled
     16 // image (the image that only returns 1.0f scale image).
     17 class GFX_EXPORT ImageSkiaRep {
     18  public:
     19   // Create null bitmap.
     20   ImageSkiaRep();
     21   ~ImageSkiaRep();
     22 
     23   // Note: This is for testing purpose only.
     24   // Creates a bitmap with kARGB_8888_Config config with given |size| in DIP.
     25   // This allocates pixels in the bitmap. It is guaranteed that the data in the
     26   // bitmap are initialized but the actual values are undefined.
     27   // Specifying 0 scale means the image is for unscaled image. (unscaled()
     28   // returns truen, and scale() returns 1.0f;)
     29   ImageSkiaRep(const gfx::Size& size, float scale);
     30 
     31   // Creates a bitmap with given scale.
     32   // Adds ref to |src|.
     33   ImageSkiaRep(const SkBitmap& src, float scale);
     34 
     35   // Returns true if the backing bitmap is null.
     36   bool is_null() const { return bitmap_.isNull(); }
     37 
     38   // Get width and height of bitmap in DIP.
     39   int GetWidth() const;
     40   int GetHeight() const;
     41 
     42   // Get width and height of bitmap in pixels.
     43   int pixel_width() const { return bitmap_.width(); }
     44   int pixel_height() const { return bitmap_.height(); }
     45   Size pixel_size() const {
     46     return Size(pixel_width(), pixel_height());
     47   }
     48 
     49   // Retrieves the scale that the bitmap will be painted at.
     50   float scale() const { return unscaled() ? 1.0f : scale_; }
     51 
     52   bool unscaled() const { return scale_ == 0.0f; }
     53 
     54   // Mark the image to be used as scaled image.
     55   void SetScaled();
     56 
     57   // Returns backing bitmap.
     58   const SkBitmap& sk_bitmap() const { return bitmap_; }
     59 
     60  private:
     61   friend class ImageSkia;
     62   SkBitmap& mutable_sk_bitmap() { return bitmap_; }
     63 
     64   SkBitmap bitmap_;
     65 
     66   float scale_;
     67 };
     68 
     69 }  // namespace gfx
     70 
     71 #endif  // UI_GFX_IMAGE_IMAGE_SKIA_REP_H_
     72