gfx/skia/trunk/include/gpu/GrTexture.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/gpu/GrTexture.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +#ifndef GrTexture_DEFINED
    1.13 +#define GrTexture_DEFINED
    1.14 +
    1.15 +#include "GrSurface.h"
    1.16 +#include "SkPoint.h"
    1.17 +#include "GrRenderTarget.h"
    1.18 +
    1.19 +class GrResourceKey;
    1.20 +class GrTextureParams;
    1.21 +
    1.22 +class GrTexture : public GrSurface {
    1.23 +
    1.24 +public:
    1.25 +    SK_DECLARE_INST_COUNT(GrTexture)
    1.26 +    // from GrResource
    1.27 +    /**
    1.28 +     * Informational texture flags
    1.29 +     */
    1.30 +    enum FlagBits {
    1.31 +        kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
    1.32 +
    1.33 +        /**
    1.34 +         * This texture should be returned to the texture cache when
    1.35 +         * it is no longer reffed
    1.36 +         */
    1.37 +        kReturnToCache_FlagBit        = kFirstBit,
    1.38 +    };
    1.39 +
    1.40 +    void setFlag(GrTextureFlags flags) {
    1.41 +        fDesc.fFlags = fDesc.fFlags | flags;
    1.42 +    }
    1.43 +    void resetFlag(GrTextureFlags flags) {
    1.44 +        fDesc.fFlags = fDesc.fFlags & ~flags;
    1.45 +    }
    1.46 +    bool isSetFlag(GrTextureFlags flags) const {
    1.47 +        return 0 != (fDesc.fFlags & flags);
    1.48 +    }
    1.49 +
    1.50 +    void dirtyMipMaps(bool mipMapsDirty) {
    1.51 +        fMipMapsDirty = mipMapsDirty;
    1.52 +    }
    1.53 +
    1.54 +    bool mipMapsAreDirty() const {
    1.55 +        return fMipMapsDirty;
    1.56 +    }
    1.57 +
    1.58 +    /**
    1.59 +     *  Approximate number of bytes used by the texture
    1.60 +     */
    1.61 +    virtual size_t sizeInBytes() const SK_OVERRIDE {
    1.62 +        return (size_t) fDesc.fWidth *
    1.63 +                        fDesc.fHeight *
    1.64 +                        GrBytesPerPixel(fDesc.fConfig);
    1.65 +    }
    1.66 +
    1.67 +    // GrSurface overrides
    1.68 +    virtual bool readPixels(int left, int top, int width, int height,
    1.69 +                            GrPixelConfig config,
    1.70 +                            void* buffer,
    1.71 +                            size_t rowBytes = 0,
    1.72 +                            uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
    1.73 +
    1.74 +    virtual void writePixels(int left, int top, int width, int height,
    1.75 +                             GrPixelConfig config,
    1.76 +                             const void* buffer,
    1.77 +                             size_t rowBytes = 0,
    1.78 +                             uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
    1.79 +
    1.80 +    /**
    1.81 +     * @return this texture
    1.82 +     */
    1.83 +    virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
    1.84 +    virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
    1.85 +
    1.86 +    /**
    1.87 +     * Retrieves the render target underlying this texture that can be passed to
    1.88 +     * GrGpu::setRenderTarget().
    1.89 +     *
    1.90 +     * @return    handle to render target or NULL if the texture is not a
    1.91 +     *            render target
    1.92 +     */
    1.93 +    virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
    1.94 +        return fRenderTarget.get();
    1.95 +    }
    1.96 +    virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
    1.97 +        return fRenderTarget.get();
    1.98 +    }
    1.99 +
   1.100 +    // GrTexture
   1.101 +    /**
   1.102 +     * Convert from texels to normalized texture coords for POT textures
   1.103 +     * only.
   1.104 +     */
   1.105 +    GrFixed normalizeFixedX(GrFixed x) const {
   1.106 +        SkASSERT(GrIsPow2(fDesc.fWidth));
   1.107 +        return x >> fShiftFixedX;
   1.108 +    }
   1.109 +    GrFixed normalizeFixedY(GrFixed y) const {
   1.110 +        SkASSERT(GrIsPow2(fDesc.fHeight));
   1.111 +        return y >> fShiftFixedY;
   1.112 +    }
   1.113 +
   1.114 +    /**
   1.115 +     *  Return the native ID or handle to the texture, depending on the
   1.116 +     *  platform. e.g. on OpenGL, return the texture ID.
   1.117 +     */
   1.118 +    virtual GrBackendObject getTextureHandle() const = 0;
   1.119 +
   1.120 +    /**
   1.121 +     *  Call this when the state of the native API texture object is
   1.122 +     *  altered directly, without being tracked by skia.
   1.123 +     */
   1.124 +    virtual void invalidateCachedState() = 0;
   1.125 +
   1.126 +#ifdef SK_DEBUG
   1.127 +    void validate() const {
   1.128 +        this->INHERITED::validate();
   1.129 +
   1.130 +        this->validateDesc();
   1.131 +    }
   1.132 +#endif
   1.133 +
   1.134 +    static GrResourceKey ComputeKey(const GrGpu* gpu,
   1.135 +                                    const GrTextureParams* params,
   1.136 +                                    const GrTextureDesc& desc,
   1.137 +                                    const GrCacheID& cacheID);
   1.138 +    static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
   1.139 +    static bool NeedsResizing(const GrResourceKey& key);
   1.140 +    static bool NeedsBilerp(const GrResourceKey& key);
   1.141 +
   1.142 +protected:
   1.143 +    // A texture refs its rt representation but not vice-versa. It is up to
   1.144 +    // the subclass constructor to initialize this pointer.
   1.145 +    SkAutoTUnref<GrRenderTarget> fRenderTarget;
   1.146 +
   1.147 +    GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
   1.148 +    : INHERITED(gpu, isWrapped, desc)
   1.149 +    , fRenderTarget(NULL)
   1.150 +    , fMipMapsDirty(true) {
   1.151 +
   1.152 +        // only make sense if alloc size is pow2
   1.153 +        fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
   1.154 +        fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
   1.155 +    }
   1.156 +    virtual ~GrTexture();
   1.157 +
   1.158 +    // GrResource overrides
   1.159 +    virtual void onRelease() SK_OVERRIDE;
   1.160 +    virtual void onAbandon() SK_OVERRIDE;
   1.161 +
   1.162 +    void validateDesc() const;
   1.163 +
   1.164 +private:
   1.165 +    // these two shift a fixed-point value into normalized coordinates
   1.166 +    // for this texture if the texture is power of two sized.
   1.167 +    int                 fShiftFixedX;
   1.168 +    int                 fShiftFixedY;
   1.169 +
   1.170 +    bool                fMipMapsDirty;
   1.171 +
   1.172 +    virtual void internal_dispose() const SK_OVERRIDE;
   1.173 +
   1.174 +    typedef GrSurface INHERITED;
   1.175 +};
   1.176 +
   1.177 +/**
   1.178 + * Represents a texture that is intended to be accessed in device coords with an offset.
   1.179 + */
   1.180 +class GrDeviceCoordTexture {
   1.181 +public:
   1.182 +    GrDeviceCoordTexture() { fOffset.set(0, 0); }
   1.183 +
   1.184 +    GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
   1.185 +        *this = other;
   1.186 +    }
   1.187 +
   1.188 +    GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
   1.189 +        : fTexture(SkSafeRef(texture))
   1.190 +        , fOffset(offset) {
   1.191 +    }
   1.192 +
   1.193 +    GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
   1.194 +        fTexture.reset(SkSafeRef(other.fTexture.get()));
   1.195 +        fOffset = other.fOffset;
   1.196 +        return *this;
   1.197 +    }
   1.198 +
   1.199 +    const SkIPoint& offset() const { return fOffset; }
   1.200 +
   1.201 +    void setOffset(const SkIPoint& offset) { fOffset = offset; }
   1.202 +    void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
   1.203 +
   1.204 +    GrTexture* texture() const { return fTexture.get(); }
   1.205 +
   1.206 +    GrTexture* setTexture(GrTexture* texture) {
   1.207 +        fTexture.reset(SkSafeRef(texture));
   1.208 +        return texture;
   1.209 +    }
   1.210 +private:
   1.211 +    SkAutoTUnref<GrTexture> fTexture;
   1.212 +    SkIPoint                fOffset;
   1.213 +};
   1.214 +
   1.215 +#endif

mercurial