| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSurface_Base.h" | 8 #include "SkSurface_Base.h" |
| 9 #include "SkImagePriv.h" | 9 #include "SkImagePriv.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkGpuDevice.h" | 11 #include "SkGpuDevice.h" |
| 12 | 12 |
| 13 class SkSurface_Gpu : public SkSurface_Base { | 13 class SkSurface_Gpu : public SkSurface_Base { |
| 14 public: | 14 public: |
| 15 SK_DECLARE_INST_COUNT(SkSurface_Gpu) | 15 SK_DECLARE_INST_COUNT(SkSurface_Gpu) |
| 16 | 16 |
| 17 SkSurface_Gpu(GrContext*, const SkImage::Info&, int sampleCount); | 17 SkSurface_Gpu(GrContext*, const SkImage::Info&, int sampleCount); |
| 18 SkSurface_Gpu(GrContext*, GrRenderTarget*); | 18 SkSurface_Gpu(GrContext*, GrRenderTarget*); |
| 19 virtual ~SkSurface_Gpu(); | 19 virtual ~SkSurface_Gpu(); |
| 20 | 20 |
| 21 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; | 21 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; |
| 22 virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE; | 22 virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE; |
| 23 virtual SkImage* onNewImageShapshot() SK_OVERRIDE; | 23 virtual SkImage* onNewImageShapshot() SK_OVERRIDE; |
| 24 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, | 24 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, |
| 25 const SkPaint*) SK_OVERRIDE; | 25 const SkPaint*) SK_OVERRIDE; |
| 26 virtual void onCopyOnWrite(SkImage*, SkCanvas*) SK_OVERRIDE; | 26 virtual void onCopyOnWrite(SkImage*, SkCanvas*, bool canDiscardContents) SK_
OVERRIDE; |
| 27 | 27 |
| 28 private: | 28 private: |
| 29 SkGpuDevice* fDevice; | 29 SkGpuDevice* fDevice; |
| 30 | 30 |
| 31 typedef SkSurface_Base INHERITED; | 31 typedef SkSurface_Base INHERITED; |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 SK_DEFINE_INST_COUNT(SkSurface_Gpu) | 34 SK_DEFINE_INST_COUNT(SkSurface_Gpu) |
| 35 | 35 |
| 36 /////////////////////////////////////////////////////////////////////////////// | 36 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget(); | 76 GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget(); |
| 77 | 77 |
| 78 return SkImage::NewTexture(rt->asTexture()); | 78 return SkImage::NewTexture(rt->asTexture()); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 81 void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 82 const SkPaint* paint) { | 82 const SkPaint* paint) { |
| 83 canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint); | 83 canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Copy the contents of the SkGpuDevice into a new texture and give that | 86 // Create a new SkGpuDevice and, if necessary, copy the contents of the old |
| 87 // texture to the SkImage. Note that this flushes the SkGpuDevice but | 87 // device into it. Note that this flushes the SkGpuDevice but |
| 88 // doesn't force an OpenGL flush. | 88 // doesn't force an OpenGL flush. |
| 89 void SkSurface_Gpu::onCopyOnWrite(SkImage* image, SkCanvas*) { | 89 void SkSurface_Gpu::onCopyOnWrite(SkImage* image, SkCanvas* canvas, bool canDisc
ardContents) { |
| 90 GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget(); | 90 GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget(); |
| 91 | 91 |
| 92 // are we sharing our render target with the image? | 92 // are we sharing our render target with the image? |
| 93 if (rt->asTexture() == SkTextureImageGetTexture(image)) { | 93 if (rt->asTexture() == SkTextureImageGetTexture(image)) { |
| 94 GrTextureDesc desc; | 94 SkGpuDevice* newDevice = static_cast<SkGpuDevice*>( |
| 95 // copyTexture requires a render target as the destination | 95 fDevice->createCompatibleDevice(fDevice->config(), fDevice->width(), |
| 96 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 96 fDevice->height(), fDevice->isOpaque())); |
| 97 desc.fWidth = fDevice->width(); | 97 SkAutoTUnref<SkGpuDevice> aurd(newDevice); |
| 98 desc.fHeight = fDevice->height(); | 98 if (!canDiscardContents) { |
| 99 desc.fConfig = SkBitmapConfig2GrPixelConfig(fDevice->config()); | 99 fDevice->context()->copyTexture(rt->asTexture(), |
| 100 desc.fSampleCnt = 0; | 100 (GrRenderTarget*)newDevice->accessRenderTarget()); |
| 101 | |
| 102 SkAutoTUnref<GrTexture> tex(fDevice->context()->createUncachedTexture(de
sc, NULL, 0)); | |
| 103 if (NULL == tex) { | |
| 104 SkTextureImageSetTexture(image, NULL); | |
| 105 return; | |
| 106 } | 101 } |
| 107 | 102 SkASSERT(NULL != canvas); |
| 108 fDevice->context()->copyTexture(rt->asTexture(), tex->asRenderTarget()); | 103 SkASSERT(canvas->getDevice() == fDevice); |
| 109 | 104 canvas->setDevice(newDevice); |
| 110 SkTextureImageSetTexture(image, tex); | 105 SkRefCnt_SafeAssign(fDevice, newDevice); |
| 111 } | 106 } |
| 112 } | 107 } |
| 113 | 108 |
| 114 /////////////////////////////////////////////////////////////////////////////// | 109 /////////////////////////////////////////////////////////////////////////////// |
| 115 | 110 |
| 116 SkSurface* SkSurface::NewRenderTargetDirect(GrContext* ctx, | 111 SkSurface* SkSurface::NewRenderTargetDirect(GrContext* ctx, |
| 117 GrRenderTarget* target) { | 112 GrRenderTarget* target) { |
| 118 if (NULL == ctx || NULL == target) { | 113 if (NULL == ctx || NULL == target) { |
| 119 return NULL; | 114 return NULL; |
| 120 } | 115 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 137 desc.fConfig = SkBitmapConfig2GrPixelConfig(config); | 132 desc.fConfig = SkBitmapConfig2GrPixelConfig(config); |
| 138 desc.fSampleCnt = sampleCount; | 133 desc.fSampleCnt = sampleCount; |
| 139 | 134 |
| 140 SkAutoTUnref<GrTexture> tex(ctx->createUncachedTexture(desc, NULL, 0)); | 135 SkAutoTUnref<GrTexture> tex(ctx->createUncachedTexture(desc, NULL, 0)); |
| 141 if (NULL == tex) { | 136 if (NULL == tex) { |
| 142 return NULL; | 137 return NULL; |
| 143 } | 138 } |
| 144 | 139 |
| 145 return SkNEW_ARGS(SkSurface_Gpu, (ctx, tex->asRenderTarget())); | 140 return SkNEW_ARGS(SkSurface_Gpu, (ctx, tex->asRenderTarget())); |
| 146 } | 141 } |
| OLD | NEW |