Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkSurface_Base.h" |
michael@0 | 9 | #include "SkImagePriv.h" |
michael@0 | 10 | #include "SkCanvas.h" |
michael@0 | 11 | #include "SkGpuDevice.h" |
michael@0 | 12 | |
michael@0 | 13 | class SkSurface_Gpu : public SkSurface_Base { |
michael@0 | 14 | public: |
michael@0 | 15 | SK_DECLARE_INST_COUNT(SkSurface_Gpu) |
michael@0 | 16 | |
michael@0 | 17 | SkSurface_Gpu(GrRenderTarget*); |
michael@0 | 18 | virtual ~SkSurface_Gpu(); |
michael@0 | 19 | |
michael@0 | 20 | virtual SkCanvas* onNewCanvas() SK_OVERRIDE; |
michael@0 | 21 | virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; |
michael@0 | 22 | virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; |
michael@0 | 23 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
michael@0 | 24 | const SkPaint*) SK_OVERRIDE; |
michael@0 | 25 | virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; |
michael@0 | 26 | |
michael@0 | 27 | private: |
michael@0 | 28 | SkGpuDevice* fDevice; |
michael@0 | 29 | |
michael@0 | 30 | typedef SkSurface_Base INHERITED; |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 34 | |
michael@0 | 35 | SkSurface_Gpu::SkSurface_Gpu(GrRenderTarget* renderTarget) |
michael@0 | 36 | : INHERITED(renderTarget->width(), renderTarget->height()) { |
michael@0 | 37 | fDevice = SkNEW_ARGS(SkGpuDevice, (renderTarget->getContext(), renderTarget)); |
michael@0 | 38 | |
michael@0 | 39 | if (kRGB_565_GrPixelConfig != renderTarget->config()) { |
michael@0 | 40 | fDevice->clear(0x0); |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | SkSurface_Gpu::~SkSurface_Gpu() { |
michael@0 | 45 | SkSafeUnref(fDevice); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | SkCanvas* SkSurface_Gpu::onNewCanvas() { |
michael@0 | 49 | return SkNEW_ARGS(SkCanvas, (fDevice)); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { |
michael@0 | 53 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
michael@0 | 54 | int sampleCount = rt->numSamples(); |
michael@0 | 55 | return SkSurface::NewRenderTarget(fDevice->context(), info, sampleCount); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | SkImage* SkSurface_Gpu::onNewImageSnapshot() { |
michael@0 | 59 | return SkImage::NewTexture(fDevice->accessBitmap(false)); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
michael@0 | 63 | const SkPaint* paint) { |
michael@0 | 64 | canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | // Create a new SkGpuDevice and, if necessary, copy the contents of the old |
michael@0 | 68 | // device into it. Note that this flushes the SkGpuDevice but |
michael@0 | 69 | // doesn't force an OpenGL flush. |
michael@0 | 70 | void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) { |
michael@0 | 71 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
michael@0 | 72 | // are we sharing our render target with the image? |
michael@0 | 73 | SkASSERT(NULL != this->getCachedImage()); |
michael@0 | 74 | if (rt->asTexture() == SkTextureImageGetTexture(this->getCachedImage())) { |
michael@0 | 75 | // We call createCompatibleDevice because it uses the texture cache. This isn't |
michael@0 | 76 | // necessarily correct (http://skbug.com/2252), but never using the cache causes |
michael@0 | 77 | // a Chromium regression. (http://crbug.com/344020) |
michael@0 | 78 | SkGpuDevice* newDevice = static_cast<SkGpuDevice*>( |
michael@0 | 79 | fDevice->createCompatibleDevice(fDevice->imageInfo())); |
michael@0 | 80 | SkAutoTUnref<SkGpuDevice> aurd(newDevice); |
michael@0 | 81 | if (kRetain_ContentChangeMode == mode) { |
michael@0 | 82 | fDevice->context()->copyTexture(rt->asTexture(), newDevice->accessRenderTarget()); |
michael@0 | 83 | } |
michael@0 | 84 | SkASSERT(NULL != this->getCachedCanvas()); |
michael@0 | 85 | SkASSERT(this->getCachedCanvas()->getDevice() == fDevice); |
michael@0 | 86 | |
michael@0 | 87 | this->getCachedCanvas()->setRootDevice(newDevice); |
michael@0 | 88 | SkRefCnt_SafeAssign(fDevice, newDevice); |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 93 | |
michael@0 | 94 | SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target) { |
michael@0 | 95 | if (NULL == target) { |
michael@0 | 96 | return NULL; |
michael@0 | 97 | } |
michael@0 | 98 | return SkNEW_ARGS(SkSurface_Gpu, (target)); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImageInfo& info, int sampleCount) { |
michael@0 | 102 | if (NULL == ctx) { |
michael@0 | 103 | return NULL; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
michael@0 | 107 | |
michael@0 | 108 | GrTextureDesc desc; |
michael@0 | 109 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kCheckAllocation_GrTextureFlagBit; |
michael@0 | 110 | desc.fWidth = info.fWidth; |
michael@0 | 111 | desc.fHeight = info.fHeight; |
michael@0 | 112 | desc.fConfig = SkBitmapConfig2GrPixelConfig(config); |
michael@0 | 113 | desc.fSampleCnt = sampleCount; |
michael@0 | 114 | |
michael@0 | 115 | SkAutoTUnref<GrTexture> tex(ctx->createUncachedTexture(desc, NULL, 0)); |
michael@0 | 116 | if (NULL == tex) { |
michael@0 | 117 | return NULL; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget())); |
michael@0 | 121 | } |