michael@0: michael@0: /* michael@0: * Copyright 2010 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: michael@0: #ifndef SkGpuDevice_DEFINED michael@0: #define SkGpuDevice_DEFINED michael@0: michael@0: #include "SkGr.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkBitmapDevice.h" michael@0: #include "SkPicture.h" michael@0: #include "SkRegion.h" michael@0: #include "GrContext.h" michael@0: michael@0: struct SkDrawProcs; michael@0: struct GrSkDrawProcs; michael@0: michael@0: class GrTextContext; michael@0: michael@0: /** michael@0: * Subclass of SkBitmapDevice, which directs all drawing to the GrGpu owned by the michael@0: * canvas. michael@0: */ michael@0: class SK_API SkGpuDevice : public SkBitmapDevice { michael@0: public: michael@0: michael@0: /** michael@0: * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render michael@0: * target. The caller owns a ref on the returned device. michael@0: */ michael@0: static SkGpuDevice* Create(GrSurface* surface); michael@0: michael@0: /** michael@0: * New device that will create an offscreen renderTarget based on the michael@0: * ImageInfo and sampleCount. The device's storage will not michael@0: * count against the GrContext's texture cache budget. The device's pixels michael@0: * will be uninitialized. On failure, returns NULL. michael@0: */ michael@0: static SkGpuDevice* Create(GrContext*, const SkImageInfo&, int sampleCount); michael@0: michael@0: #ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG michael@0: /** michael@0: * New device that will create an offscreen renderTarget based on the michael@0: * config, width, height, and sampleCount. The device's storage will not michael@0: * count against the GrContext's texture cache budget. The device's pixels michael@0: * will be uninitialized. TODO: This can fail, replace with a factory function. michael@0: */ michael@0: SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, int sampleCount = 0); michael@0: #endif michael@0: michael@0: /** michael@0: * DEPRECATED -- need to make this private, call Create(surface) michael@0: * New device that will render to the specified renderTarget. michael@0: */ michael@0: SkGpuDevice(GrContext*, GrRenderTarget*); michael@0: michael@0: /** michael@0: * DEPRECATED -- need to make this private, call Create(surface) michael@0: * New device that will render to the texture (as a rendertarget). michael@0: * The GrTexture's asRenderTarget() must be non-NULL or device will not michael@0: * function. michael@0: */ michael@0: SkGpuDevice(GrContext*, GrTexture*); michael@0: michael@0: virtual ~SkGpuDevice(); michael@0: michael@0: GrContext* context() const { return fContext; } michael@0: michael@0: virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE; michael@0: michael@0: // overrides from SkBaseDevice michael@0: virtual int width() const SK_OVERRIDE { michael@0: return NULL == fRenderTarget ? 0 : fRenderTarget->width(); michael@0: } michael@0: virtual int height() const SK_OVERRIDE { michael@0: return NULL == fRenderTarget ? 0 : fRenderTarget->height(); michael@0: } michael@0: virtual bool isOpaque() const SK_OVERRIDE { michael@0: return NULL == fRenderTarget ? false michael@0: : kRGB_565_GrPixelConfig == fRenderTarget->config(); michael@0: } michael@0: virtual SkBitmap::Config config() const SK_OVERRIDE; michael@0: michael@0: virtual void clear(SkColor color) SK_OVERRIDE; michael@0: #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG michael@0: virtual void writePixels(const SkBitmap& bitmap, int x, int y, michael@0: SkCanvas::Config8888 config8888) SK_OVERRIDE; michael@0: #endif michael@0: virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE; michael@0: virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, michael@0: const SkPoint[], const SkPaint& paint) SK_OVERRIDE; michael@0: virtual void drawRect(const SkDraw&, const SkRect& r, michael@0: const SkPaint& paint) SK_OVERRIDE; michael@0: virtual void drawRRect(const SkDraw&, const SkRRect& r, michael@0: const SkPaint& paint) SK_OVERRIDE; michael@0: virtual void drawOval(const SkDraw&, const SkRect& oval, michael@0: const SkPaint& paint) SK_OVERRIDE; michael@0: virtual void drawPath(const SkDraw&, const SkPath& path, michael@0: const SkPaint& paint, const SkMatrix* prePathMatrix, michael@0: bool pathIsMutable) SK_OVERRIDE; michael@0: virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, michael@0: const SkMatrix&, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, michael@0: const SkRect* srcOrNull, const SkRect& dst, michael@0: const SkPaint& paint, michael@0: SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE; michael@0: virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, michael@0: int x, int y, const SkPaint& paint); michael@0: virtual void drawText(const SkDraw&, const void* text, size_t len, michael@0: SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawPosText(const SkDraw&, const void* text, size_t len, michael@0: const SkScalar pos[], SkScalar constY, michael@0: int scalarsPerPos, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, michael@0: const SkPath& path, const SkMatrix* matrix, michael@0: const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, michael@0: const SkPoint verts[], const SkPoint texs[], michael@0: const SkColor colors[], SkXfermode* xmode, michael@0: const uint16_t indices[], int indexCount, michael@0: const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, michael@0: const SkPaint&) SK_OVERRIDE; michael@0: virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE; michael@0: michael@0: virtual void flush() SK_OVERRIDE; michael@0: michael@0: virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE; michael@0: virtual void onDetachFromCanvas() SK_OVERRIDE; michael@0: michael@0: /** michael@0: * Make's this device's rendertarget current in the underlying 3D API. michael@0: * Also implicitly flushes. michael@0: */ michael@0: virtual void makeRenderTargetCurrent(); michael@0: michael@0: virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE; michael@0: virtual bool filterImage(const SkImageFilter*, const SkBitmap&, michael@0: const SkImageFilter::Context&, michael@0: SkBitmap*, SkIPoint*) SK_OVERRIDE; michael@0: michael@0: class SkAutoCachedTexture; // used internally michael@0: michael@0: michael@0: protected: michael@0: virtual bool onReadPixels(const SkBitmap&, int x, int y, SkCanvas::Config8888) SK_OVERRIDE; michael@0: virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE; michael@0: michael@0: /** PRIVATE / EXPERIMENTAL -- do not call */ michael@0: virtual void EXPERIMENTAL_optimize(SkPicture* picture) SK_OVERRIDE; michael@0: /** PRIVATE / EXPERIMENTAL -- do not call */ michael@0: virtual bool EXPERIMENTAL_drawPicture(const SkPicture& picture) SK_OVERRIDE; michael@0: michael@0: private: michael@0: GrContext* fContext; michael@0: michael@0: GrSkDrawProcs* fDrawProcs; michael@0: michael@0: GrClipData fClipData; michael@0: michael@0: GrTextContext* fMainTextContext; michael@0: GrTextContext* fFallbackTextContext; michael@0: michael@0: // state for our render-target michael@0: GrRenderTarget* fRenderTarget; michael@0: bool fNeedClear; michael@0: michael@0: // called from rt and tex cons michael@0: void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached); michael@0: michael@0: // used by createCompatibleDevice michael@0: SkGpuDevice(GrContext*, GrTexture* texture, bool needClear); michael@0: michael@0: virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE; michael@0: michael@0: virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE; michael@0: michael@0: // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override michael@0: // SkDraw's matrix and draw in device coords. michael@0: void prepareDraw(const SkDraw&, bool forceIdentity); michael@0: michael@0: /** michael@0: * Implementation for both drawBitmap and drawBitmapRect. michael@0: */ michael@0: void drawBitmapCommon(const SkDraw&, michael@0: const SkBitmap& bitmap, michael@0: const SkRect* srcRectPtr, michael@0: const SkSize* dstSizePtr, // ignored iff srcRectPtr == NULL michael@0: const SkPaint&, michael@0: SkCanvas::DrawBitmapRectFlags flags); michael@0: michael@0: /** michael@0: * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's michael@0: * matrix, clip, and the device's render target has already been set on GrContext. michael@0: */ michael@0: michael@0: // The tileSize and clippedSrcRect will be valid only if true is returned. michael@0: bool shouldTileBitmap(const SkBitmap& bitmap, michael@0: const GrTextureParams& sampler, michael@0: const SkRect* srcRectPtr, michael@0: int maxTileSize, michael@0: int* tileSize, michael@0: SkIRect* clippedSrcRect) const; michael@0: void internalDrawBitmap(const SkBitmap&, michael@0: const SkRect&, michael@0: const GrTextureParams& params, michael@0: const SkPaint& paint, michael@0: SkCanvas::DrawBitmapRectFlags flags, michael@0: bool bicubic); michael@0: void drawTiledBitmap(const SkBitmap& bitmap, michael@0: const SkRect& srcRect, michael@0: const SkIRect& clippedSrcRect, michael@0: const GrTextureParams& params, michael@0: const SkPaint& paint, michael@0: SkCanvas::DrawBitmapRectFlags flags, michael@0: int tileSize, michael@0: bool bicubic); michael@0: michael@0: static SkPicture::AccelData::Key ComputeAccelDataKey(); michael@0: michael@0: typedef SkBitmapDevice INHERITED; michael@0: }; michael@0: michael@0: #endif