michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef GrSurface_DEFINED michael@0: #define GrSurface_DEFINED michael@0: michael@0: #include "GrTypes.h" michael@0: #include "GrResource.h" michael@0: #include "SkRect.h" michael@0: michael@0: class GrTexture; michael@0: class GrRenderTarget; michael@0: struct SkImageInfo; michael@0: michael@0: class GrSurface : public GrResource { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(GrSurface); michael@0: michael@0: /** michael@0: * Retrieves the width of the surface. michael@0: * michael@0: * @return the width in texels michael@0: */ michael@0: int width() const { return fDesc.fWidth; } michael@0: michael@0: /** michael@0: * Retrieves the height of the surface. michael@0: * michael@0: * @return the height in texels michael@0: */ michael@0: int height() const { return fDesc.fHeight; } michael@0: michael@0: /** michael@0: * Helper that gets the width and height of the surface as a bounding rectangle. michael@0: */ michael@0: void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()), michael@0: SkIntToScalar(this->height())); } michael@0: michael@0: GrSurfaceOrigin origin() const { michael@0: SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin); michael@0: return fDesc.fOrigin; michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the pixel config specified when the surface was created. michael@0: * For render targets this can be kUnknown_GrPixelConfig michael@0: * if client asked us to render to a target that has a pixel michael@0: * config that isn't equivalent with one of our configs. michael@0: */ michael@0: GrPixelConfig config() const { return fDesc.fConfig; } michael@0: michael@0: /** michael@0: * Return the descriptor describing the surface michael@0: */ michael@0: const GrTextureDesc& desc() const { return fDesc; } michael@0: michael@0: void asImageInfo(SkImageInfo*) const; michael@0: michael@0: /** michael@0: * @return the texture associated with the surface, may be NULL. michael@0: */ michael@0: virtual GrTexture* asTexture() = 0; michael@0: virtual const GrTexture* asTexture() const = 0; michael@0: michael@0: /** michael@0: * @return the render target underlying this surface, may be NULL. michael@0: */ michael@0: virtual GrRenderTarget* asRenderTarget() = 0; michael@0: virtual const GrRenderTarget* asRenderTarget() const = 0; michael@0: michael@0: /** michael@0: * Checks whether this GrSurface refers to the same GPU object as other. This michael@0: * catches the case where a GrTexture and GrRenderTarget refer to the same michael@0: * GPU memory. michael@0: */ michael@0: bool isSameAs(const GrSurface* other) const { michael@0: const GrRenderTarget* thisRT = this->asRenderTarget(); michael@0: if (NULL != thisRT) { michael@0: return thisRT == other->asRenderTarget(); michael@0: } else { michael@0: const GrTexture* thisTex = this->asTexture(); michael@0: SkASSERT(NULL != thisTex); // We must be one or the other michael@0: return thisTex == other->asTexture(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Reads a rectangle of pixels from the surface. michael@0: * @param left left edge of the rectangle to read (inclusive) michael@0: * @param top top edge of the rectangle to read (inclusive) michael@0: * @param width width of rectangle to read in pixels. michael@0: * @param height height of rectangle to read in pixels. michael@0: * @param config the pixel config of the destination buffer michael@0: * @param buffer memory to read the rectangle into. michael@0: * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly michael@0: * packed. michael@0: * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum. michael@0: * michael@0: * @return true if the read succeeded, false if not. The read can fail because of an unsupported michael@0: * pixel config. michael@0: */ michael@0: virtual bool readPixels(int left, int top, int width, int height, michael@0: GrPixelConfig config, michael@0: void* buffer, michael@0: size_t rowBytes = 0, michael@0: uint32_t pixelOpsFlags = 0) = 0; michael@0: michael@0: /** michael@0: * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified michael@0: * rectangle. michael@0: * @param left left edge of the rectangle to write (inclusive) michael@0: * @param top top edge of the rectangle to write (inclusive) michael@0: * @param width width of rectangle to write in pixels. michael@0: * @param height height of rectangle to write in pixels. michael@0: * @param config the pixel config of the source buffer michael@0: * @param buffer memory to read the rectangle from. michael@0: * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly michael@0: * packed. michael@0: * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum. michael@0: */ michael@0: virtual void writePixels(int left, int top, int width, int height, michael@0: GrPixelConfig config, michael@0: const void* buffer, michael@0: size_t rowBytes = 0, michael@0: uint32_t pixelOpsFlags = 0) = 0; michael@0: michael@0: /** michael@0: * Write the contents of the surface to a PNG. Returns true if successful. michael@0: * @param filename Full path to desired file michael@0: */ michael@0: bool savePixels(const char* filename); michael@0: michael@0: protected: michael@0: GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) michael@0: : INHERITED(gpu, isWrapped) michael@0: , fDesc(desc) { michael@0: } michael@0: michael@0: GrTextureDesc fDesc; michael@0: michael@0: private: michael@0: typedef GrResource INHERITED; michael@0: }; michael@0: michael@0: #endif // GrSurface_DEFINED