reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 8 | #include "SkSurface_Gpu.h" |
| 9 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 10 | #include "GrGpuResourcePriv.h" |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 12 | #include "SkGpuDevice.h" |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 13 | #include "SkImage_Base.h" |
| 14 | #include "SkImagePriv.h" |
| 15 | #include "SkSurface_Base.h" |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 16 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
| 18 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 21 | SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device) |
| 22 | : INHERITED(device->width(), device->height(), &device->surfaceProps()) |
| 23 | , fDevice(SkRef(device)) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 24 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 25 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 26 | SkSurface_Gpu::~SkSurface_Gpu() { |
| 27 | SkSafeUnref(fDevice); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | SkCanvas* SkSurface_Gpu::onNewCanvas() { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 31 | SkCanvas::InitFlags flags = SkCanvas::kDefault_InitFlags; |
| 32 | // When we think this works... |
| 33 | // flags |= SkCanvas::kConservativeRasterClip_InitFlag; |
| 34 | |
| 35 | return SkNEW_ARGS(SkCanvas, (fDevice, &this->props(), flags)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 36 | } |
| 37 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 38 | SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 39 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 40 | int sampleCount = rt->numSamples(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 41 | // TODO: Make caller specify this (change virtual signature of onNewSurface). |
| 42 | static const Budgeted kBudgeted = kNo_Budgeted; |
| 43 | return SkSurface::NewRenderTarget(fDevice->context(), kBudgeted, info, sampleCount, |
| 44 | &this->props()); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 47 | SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 48 | const int sampleCount = fDevice->accessRenderTarget()->numSamples(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 49 | SkImage* image = SkNewImageFromBitmapTexture(fDevice->accessBitmap(false), sampleCount, |
| 50 | budgeted); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 51 | if (image) { |
| 52 | as_IB(image)->initWithProps(this->props()); |
| 53 | } |
| 54 | return image; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 58 | const SkPaint* paint) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 59 | canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint); |
| 60 | } |
| 61 | |
junov@chromium.org | 45c3db8 | 2013-04-11 17:52:05 +0000 | [diff] [blame] | 62 | // Create a new SkGpuDevice and, if necessary, copy the contents of the old |
| 63 | // device into it. Note that this flushes the SkGpuDevice but |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 64 | // doesn't force an OpenGL flush. |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 65 | void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 66 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 67 | // are we sharing our render target with the image? Note this call should never create a new |
| 68 | // image because onCopyOnWrite is only called when there is a cached image. |
| 69 | SkImage* image = this->getCachedImage(kNo_Budgeted); |
| 70 | SkASSERT(image); |
| 71 | if (rt->asTexture() == SkTextureImageGetTexture(image)) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 72 | GrRenderTarget* oldRT = this->fDevice->accessRenderTarget(); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 73 | SkSurface::Budgeted budgeted = oldRT->resourcePriv().isBudgeted() ? kYes_Budgeted : |
| 74 | kNo_Budgeted; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 75 | SkAutoTUnref<SkGpuDevice> newDevice( |
| 76 | SkGpuDevice::Create(oldRT->getContext(), budgeted, fDevice->imageInfo(), |
| 77 | oldRT->numSamples(), &this->props(), 0)); |
| 78 | if (kRetain_ContentChangeMode == mode && !oldRT->wasDestroyed() && newDevice) { |
| 79 | oldRT->getContext()->copySurface(newDevice->accessRenderTarget(), oldRT); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 80 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 81 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 82 | SkASSERT(this->getCachedCanvas()); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 83 | SkASSERT(this->getCachedCanvas()->getDevice() == fDevice); |
commit-bot@chromium.org | e492ac4 | 2014-03-04 22:37:29 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 85 | this->getCachedCanvas()->setRootDevice(newDevice); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 86 | SkRefCnt_SafeAssign(fDevice, newDevice.get()); |
| 87 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 88 | SkTextureImageApplyBudgetedDecision(image); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 89 | } else if (kDiscard_ContentChangeMode == mode) { |
| 90 | this->SkSurface_Gpu::onDiscard(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 91 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 92 | } |
| 93 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 94 | void SkSurface_Gpu::onDiscard() { |
| 95 | fDevice->accessRenderTarget()->discard(); |
| 96 | } |
| 97 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 100 | SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurfaceProps* props) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 101 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target, props)); |
| 102 | if (!device) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 105 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 106 | } |
| 107 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 108 | SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, Budgeted budgeted, const SkImageInfo& info, |
| 109 | int sampleCount, const SkSurfaceProps* props) { |
| 110 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(ctx, budgeted, info, sampleCount, props, |
| 111 | SkGpuDevice::kNeedClear_Flag)); |
| 112 | if (!device) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 115 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 116 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 117 | |
| 118 | #endif |