gfx/skia/trunk/include/core/SkImage.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkImage.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/*
     1.5 + * Copyright 2012 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkImage_DEFINED
    1.12 +#define SkImage_DEFINED
    1.13 +
    1.14 +#include "SkImageInfo.h"
    1.15 +#include "SkImageEncoder.h"
    1.16 +#include "SkRefCnt.h"
    1.17 +#include "SkScalar.h"
    1.18 +
    1.19 +class SkData;
    1.20 +class SkCanvas;
    1.21 +class SkPaint;
    1.22 +class SkShader;
    1.23 +class GrContext;
    1.24 +class GrTexture;
    1.25 +
    1.26 +// need for TileMode
    1.27 +#include "SkShader.h"
    1.28 +
    1.29 +/**
    1.30 + *  SkImage is an abstraction for drawing a rectagle of pixels, though the
    1.31 + *  particular type of image could be actually storing its data on the GPU, or
    1.32 + *  as drawing commands (picture or PDF or otherwise), ready to be played back
    1.33 + *  into another canvas.
    1.34 + *
    1.35 + *  The content of SkImage is always immutable, though the actual storage may
    1.36 + *  change, if for example that image can be re-created via encoded data or
    1.37 + *  other means.
    1.38 + */
    1.39 +class SK_API SkImage : public SkRefCnt {
    1.40 +public:
    1.41 +    SK_DECLARE_INST_COUNT(SkImage)
    1.42 +
    1.43 +    typedef SkImageInfo Info;
    1.44 +
    1.45 +    static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes);
    1.46 +    static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
    1.47 +    static SkImage* NewEncodedData(SkData*);
    1.48 +
    1.49 +    /**
    1.50 +     * GrTexture is a more logical parameter for this factory, but its
    1.51 +     * interactions with scratch cache still has issues, so for now we take
    1.52 +     * SkBitmap instead. This will be changed in the future. skbug.com/1449
    1.53 +     */
    1.54 +    static SkImage* NewTexture(const SkBitmap&);
    1.55 +
    1.56 +    int width() const { return fWidth; }
    1.57 +    int height() const { return fHeight; }
    1.58 +    uint32_t uniqueID() const { return fUniqueID; }
    1.59 +
    1.60 +    /**
    1.61 +     * Return the GrTexture that stores the image pixels. Calling getTexture
    1.62 +     * does not affect the reference count of the GrTexture object.
    1.63 +     * Will return NULL if the image does not use a texture.
    1.64 +     */
    1.65 +    GrTexture* getTexture();
    1.66 +
    1.67 +    SkShader*   newShaderClamp() const;
    1.68 +    SkShader*   newShader(SkShader::TileMode, SkShader::TileMode) const;
    1.69 +
    1.70 +    void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
    1.71 +
    1.72 +    /**
    1.73 +     *  Draw the image, cropped to the src rect, to the dst rect of a canvas.
    1.74 +     *  If src is larger than the bounds of the image, the rest of the image is
    1.75 +     *  filled with transparent black pixels.
    1.76 +     *
    1.77 +     *  See SkCanvas::drawBitmapRectToRect for similar behavior.
    1.78 +     */
    1.79 +    void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*);
    1.80 +
    1.81 +    /**
    1.82 +     *  If the image has direct access to its pixels (i.e. they are in local
    1.83 +     *  RAM) return the (const) address of those pixels, and if not null, return
    1.84 +     *  the ImageInfo and rowBytes. The returned address is only valid while
    1.85 +     *  the image object is in scope.
    1.86 +     *
    1.87 +     *  On failure, returns NULL and the info and rowBytes parameters are
    1.88 +     *  ignored.
    1.89 +     */
    1.90 +    const void* peekPixels(SkImageInfo* info, size_t* rowBytes) const;
    1.91 +
    1.92 +    /**
    1.93 +     *  Encode the image's pixels and return the result as a new SkData, which
    1.94 +     *  the caller must manage (i.e. call unref() when they are done).
    1.95 +     *
    1.96 +     *  If the image type cannot be encoded, or the requested encoder type is
    1.97 +     *  not supported, this will return NULL.
    1.98 +     */
    1.99 +    SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type,
   1.100 +                   int quality = 80) const;
   1.101 +
   1.102 +protected:
   1.103 +    SkImage(int width, int height) :
   1.104 +        fWidth(width),
   1.105 +        fHeight(height),
   1.106 +        fUniqueID(NextUniqueID()) {
   1.107 +
   1.108 +        SkASSERT(width >= 0);
   1.109 +        SkASSERT(height >= 0);
   1.110 +    }
   1.111 +
   1.112 +private:
   1.113 +    const int       fWidth;
   1.114 +    const int       fHeight;
   1.115 +    const uint32_t  fUniqueID;
   1.116 +
   1.117 +    static uint32_t NextUniqueID();
   1.118 +
   1.119 +    typedef SkRefCnt INHERITED;
   1.120 +
   1.121 +    /**
   1.122 +     *  Return a copy of the image's pixels, limiting them to the subset
   1.123 +     *  rectangle's intersection wit the image bounds. If subset is NULL, then
   1.124 +     *  the entire image will be considered.
   1.125 +     *
   1.126 +     *  If the bitmap's pixels have already been allocated, then readPixels()
   1.127 +     *  will succeed only if it can support converting the image's pixels into
   1.128 +     *  the bitmap's ColorType/AlphaType. Any pixels in the bitmap that do not
   1.129 +     *  intersect with the image's bounds and the subset (if not null) will be
   1.130 +     *  left untouched.
   1.131 +     *
   1.132 +     *  If the bitmap is initially empty/unallocated, then it will be allocated
   1.133 +     *  using the default allocator, and the ColorType/AlphaType will be chosen
   1.134 +     *  to most closely fit the image's configuration.
   1.135 +     *
   1.136 +     *  On failure, false will be returned, and bitmap will unmodified.
   1.137 +     */
   1.138 +    // On ice for now:
   1.139 +    // - should it respect the particular colortype/alphatype of the src
   1.140 +    // - should it have separate entrypoints for preallocated and not bitmaps?
   1.141 +    // - isn't it enough to allow the caller to draw() the image into a canvas?
   1.142 +    bool readPixels(SkBitmap* bitmap, const SkIRect* subset = NULL) const;
   1.143 +};
   1.144 +
   1.145 +#endif

mercurial