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 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 11 | #include "SkGpuDevice.h" |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 12 | #include "SkImage_Base.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 13 | #include "SkImage_Gpu.h" |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 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 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 20 | SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device) |
| 21 | : INHERITED(device->width(), device->height(), &device->surfaceProps()) |
| 22 | , fDevice(SkRef(device)) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 23 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 24 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 25 | SkSurface_Gpu::~SkSurface_Gpu() { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 26 | fDevice->unref(); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | SkCanvas* SkSurface_Gpu::onNewCanvas() { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 30 | SkCanvas::InitFlags flags = SkCanvas::kDefault_InitFlags; |
| 31 | // When we think this works... |
| 32 | // flags |= SkCanvas::kConservativeRasterClip_InitFlag; |
| 33 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 34 | return SkNEW_ARGS(SkCanvas, (fDevice, flags)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 37 | SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 38 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 39 | int sampleCount = rt->numColorSamples(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 40 | // TODO: Make caller specify this (change virtual signature of onNewSurface). |
| 41 | static const Budgeted kBudgeted = kNo_Budgeted; |
| 42 | return SkSurface::NewRenderTarget(fDevice->context(), kBudgeted, info, sampleCount, |
| 43 | &this->props()); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 46 | SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 47 | const SkImageInfo info = fDevice->imageInfo(); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 48 | const int sampleCount = fDevice->accessRenderTarget()->numColorSamples(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 49 | SkImage* image = NULL; |
| 50 | GrTexture* tex = fDevice->accessRenderTarget()->asTexture(); |
| 51 | if (tex) { |
| 52 | image = SkNEW_ARGS(SkImage_Gpu, |
| 53 | (info.width(), info.height(), info.alphaType(), |
| 54 | tex, sampleCount, budgeted)); |
| 55 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 56 | if (image) { |
| 57 | as_IB(image)->initWithProps(this->props()); |
| 58 | } |
| 59 | return image; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 62 | // Create a new render target and, if necessary, copy the contents of the old |
| 63 | // render target 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); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 71 | if (rt->asTexture() == as_IB(image)->getTexture()) { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 72 | this->fDevice->replaceRenderTarget(SkSurface::kRetain_ContentChangeMode == mode); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 73 | SkTextureImageApplyBudgetedDecision(image); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 74 | } else if (kDiscard_ContentChangeMode == mode) { |
| 75 | this->SkSurface_Gpu::onDiscard(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 76 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 79 | void SkSurface_Gpu::onDiscard() { |
| 80 | fDevice->accessRenderTarget()->discard(); |
| 81 | } |
| 82 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 85 | SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurfaceProps* props) { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 86 | SkAutoTUnref<SkGpuDevice> device( |
| 87 | SkGpuDevice::Create(target, props, SkGpuDevice::kUninit_InitContents)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 88 | if (!device) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 89 | return NULL; |
| 90 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 91 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 94 | SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, Budgeted budgeted, const SkImageInfo& info, |
| 95 | int sampleCount, const SkSurfaceProps* props) { |
| 96 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(ctx, budgeted, info, sampleCount, props, |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 97 | SkGpuDevice::kClear_InitContents)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 98 | if (!device) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 101 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 102 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 103 | |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 104 | SkSurface* SkSurface::NewWrappedRenderTarget(GrContext* context, GrBackendTextureDesc desc, |
| 105 | const SkSurfaceProps* props) { |
| 106 | if (NULL == context) { |
| 107 | return NULL; |
| 108 | } |
| 109 | if (!SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) { |
| 110 | return NULL; |
| 111 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 112 | SkAutoTUnref<GrSurface> surface(context->textureProvider()->wrapBackendTexture(desc, |
| 113 | kBorrow_GrWrapOwnership)); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 114 | if (!surface) { |
| 115 | return NULL; |
| 116 | } |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 117 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(surface->asRenderTarget(), props, |
| 118 | SkGpuDevice::kUninit_InitContents)); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 119 | if (!device) { |
| 120 | return NULL; |
| 121 | } |
| 122 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
| 123 | } |
| 124 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 125 | #endif |