gfx/skia/trunk/src/image/SkSurface_Gpu.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/image/SkSurface_Gpu.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     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 +#include "SkSurface_Base.h"
    1.12 +#include "SkImagePriv.h"
    1.13 +#include "SkCanvas.h"
    1.14 +#include "SkGpuDevice.h"
    1.15 +
    1.16 +class SkSurface_Gpu : public SkSurface_Base {
    1.17 +public:
    1.18 +    SK_DECLARE_INST_COUNT(SkSurface_Gpu)
    1.19 +
    1.20 +    SkSurface_Gpu(GrRenderTarget*);
    1.21 +    virtual ~SkSurface_Gpu();
    1.22 +
    1.23 +    virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
    1.24 +    virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
    1.25 +    virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
    1.26 +    virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
    1.27 +                        const SkPaint*) SK_OVERRIDE;
    1.28 +    virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE;
    1.29 +
    1.30 +private:
    1.31 +    SkGpuDevice* fDevice;
    1.32 +
    1.33 +    typedef SkSurface_Base INHERITED;
    1.34 +};
    1.35 +
    1.36 +///////////////////////////////////////////////////////////////////////////////
    1.37 +
    1.38 +SkSurface_Gpu::SkSurface_Gpu(GrRenderTarget* renderTarget)
    1.39 +        : INHERITED(renderTarget->width(), renderTarget->height()) {
    1.40 +    fDevice = SkNEW_ARGS(SkGpuDevice, (renderTarget->getContext(), renderTarget));
    1.41 +
    1.42 +    if (kRGB_565_GrPixelConfig != renderTarget->config()) {
    1.43 +        fDevice->clear(0x0);
    1.44 +    }
    1.45 +}
    1.46 +
    1.47 +SkSurface_Gpu::~SkSurface_Gpu() {
    1.48 +    SkSafeUnref(fDevice);
    1.49 +}
    1.50 +
    1.51 +SkCanvas* SkSurface_Gpu::onNewCanvas() {
    1.52 +    return SkNEW_ARGS(SkCanvas, (fDevice));
    1.53 +}
    1.54 +
    1.55 +SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) {
    1.56 +    GrRenderTarget* rt = fDevice->accessRenderTarget();
    1.57 +    int sampleCount = rt->numSamples();
    1.58 +    return SkSurface::NewRenderTarget(fDevice->context(), info, sampleCount);
    1.59 +}
    1.60 +
    1.61 +SkImage* SkSurface_Gpu::onNewImageSnapshot() {
    1.62 +    return SkImage::NewTexture(fDevice->accessBitmap(false));
    1.63 +}
    1.64 +
    1.65 +void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
    1.66 +                              const SkPaint* paint) {
    1.67 +    canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint);
    1.68 +}
    1.69 +
    1.70 +// Create a new SkGpuDevice and, if necessary, copy the contents of the old
    1.71 +// device into it. Note that this flushes the SkGpuDevice but
    1.72 +// doesn't force an OpenGL flush.
    1.73 +void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) {
    1.74 +    GrRenderTarget* rt = fDevice->accessRenderTarget();
    1.75 +    // are we sharing our render target with the image?
    1.76 +    SkASSERT(NULL != this->getCachedImage());
    1.77 +    if (rt->asTexture() == SkTextureImageGetTexture(this->getCachedImage())) {
    1.78 +        // We call createCompatibleDevice because it uses the texture cache. This isn't
    1.79 +        // necessarily correct (http://skbug.com/2252), but never using the cache causes
    1.80 +        // a Chromium regression. (http://crbug.com/344020)
    1.81 +        SkGpuDevice* newDevice = static_cast<SkGpuDevice*>(
    1.82 +            fDevice->createCompatibleDevice(fDevice->imageInfo()));
    1.83 +        SkAutoTUnref<SkGpuDevice> aurd(newDevice);
    1.84 +        if (kRetain_ContentChangeMode == mode) {
    1.85 +            fDevice->context()->copyTexture(rt->asTexture(), newDevice->accessRenderTarget());
    1.86 +        }
    1.87 +        SkASSERT(NULL != this->getCachedCanvas());
    1.88 +        SkASSERT(this->getCachedCanvas()->getDevice() == fDevice);
    1.89 +
    1.90 +        this->getCachedCanvas()->setRootDevice(newDevice);
    1.91 +        SkRefCnt_SafeAssign(fDevice, newDevice);
    1.92 +    }
    1.93 +}
    1.94 +
    1.95 +///////////////////////////////////////////////////////////////////////////////
    1.96 +
    1.97 +SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target) {
    1.98 +    if (NULL == target) {
    1.99 +        return NULL;
   1.100 +    }
   1.101 +    return SkNEW_ARGS(SkSurface_Gpu, (target));
   1.102 +}
   1.103 +
   1.104 +SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImageInfo& info, int sampleCount) {
   1.105 +    if (NULL == ctx) {
   1.106 +        return NULL;
   1.107 +    }
   1.108 +
   1.109 +    SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
   1.110 +
   1.111 +    GrTextureDesc desc;
   1.112 +    desc.fFlags = kRenderTarget_GrTextureFlagBit | kCheckAllocation_GrTextureFlagBit;
   1.113 +    desc.fWidth = info.fWidth;
   1.114 +    desc.fHeight = info.fHeight;
   1.115 +    desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
   1.116 +    desc.fSampleCnt = sampleCount;
   1.117 +
   1.118 +    SkAutoTUnref<GrTexture> tex(ctx->createUncachedTexture(desc, NULL, 0));
   1.119 +    if (NULL == tex) {
   1.120 +        return NULL;
   1.121 +    }
   1.122 +
   1.123 +    return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget()));
   1.124 +}

mercurial