Home | History | Annotate | Download | only in image
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkImage_Base_DEFINED
      9 #define SkImage_Base_DEFINED
     10 
     11 #include "SkImage.h"
     12 #include "SkSurface.h"
     13 
     14 static SkSurfaceProps copy_or_safe_defaults(const SkSurfaceProps* props) {
     15     return props ? *props : SkSurfaceProps(0, kUnknown_SkPixelGeometry);
     16 }
     17 
     18 class SkImage_Base : public SkImage {
     19 public:
     20     SkImage_Base(int width, int height, const SkSurfaceProps* props)
     21         : INHERITED(width, height)
     22         , fProps(copy_or_safe_defaults(props))
     23     {}
     24 
     25     /**
     26      *  If the props weren't know at constructor time, call this but only before the image is
     27      *  ever released into the wild (since the props field must appear to be immutable).
     28      */
     29     void initWithProps(const SkSurfaceProps& props) {
     30         SkASSERT(this->unique());   // only viewed by one thread
     31         SkSurfaceProps* mutableProps = const_cast<SkSurfaceProps*>(&fProps);
     32         SkASSERT(mutableProps != &props);   // check for self-assignment
     33         mutableProps->~SkSurfaceProps();
     34         new (mutableProps) SkSurfaceProps(props);
     35     }
     36 
     37     const SkSurfaceProps& props() const { return fProps; }
     38 
     39     virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0;
     40 
     41     virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const {
     42         return NULL;
     43     }
     44 
     45     // Default impl calls onDraw
     46     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
     47                               int srcX, int srcY) const;
     48 
     49     virtual GrTexture* onGetTexture() const { return NULL; }
     50 
     51     // return a read-only copy of the pixels. We promise to not modify them,
     52     // but only inspect them (or encode them).
     53     virtual bool getROPixels(SkBitmap*) const { return false; }
     54 
     55     virtual SkShader* onNewShader(SkShader::TileMode,
     56                                   SkShader::TileMode,
     57                                   const SkMatrix* localMatrix) const { return NULL; };
     58 
     59     // newWidth > 0, newHeight > 0, subset either NULL or a proper subset of this bounds
     60     virtual SkImage* onNewImage(int newWidth, int newHeight, const SkIRect* subset,
     61                                 SkFilterQuality) const;
     62 
     63 private:
     64     const SkSurfaceProps fProps;
     65 
     66     typedef SkImage INHERITED;
     67 };
     68 
     69 static inline SkImage_Base* as_IB(SkImage* image) {
     70     return static_cast<SkImage_Base*>(image);
     71 }
     72 
     73 static inline const SkImage_Base* as_IB(const SkImage* image) {
     74     return static_cast<const SkImage_Base*>(image);
     75 }
     76 
     77 #endif
     78