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 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 19 | SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device) |
| 20 | : INHERITED(device->width(), device->height(), &device->surfaceProps()) |
| 21 | , fDevice(SkRef(device)) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 22 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 23 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 24 | SkSurface_Gpu::~SkSurface_Gpu() { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 25 | fDevice->unref(); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 26 | } |
| 27 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 28 | static GrRenderTarget* prepare_rt_for_external_access(SkSurface_Gpu* surface, |
| 29 | SkSurface::BackendHandleAccess access) { |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 30 | switch (access) { |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 31 | case SkSurface::kFlushRead_BackendHandleAccess: |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 32 | break; |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 33 | case SkSurface::kFlushWrite_BackendHandleAccess: |
| 34 | case SkSurface::kDiscardWrite_BackendHandleAccess: |
reed | 884200e | 2015-06-29 09:00:20 -0700 | [diff] [blame] | 35 | // for now we don't special-case on Discard, but we may in the future. |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 36 | surface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode); |
reed | dca20ce | 2015-07-03 10:43:43 -0700 | [diff] [blame] | 37 | // legacy: need to dirty the bitmap's genID in our device (curse it) |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 38 | surface->getDevice()->accessBitmap(false).notifyPixelsChanged(); |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 39 | break; |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 40 | } |
fmalita | e263908 | 2015-08-06 07:04:51 -0700 | [diff] [blame] | 41 | |
| 42 | // Grab the render target *after* firing notifications, as it may get switched if CoW kicks in. |
| 43 | GrRenderTarget* rt = surface->getDevice()->accessRenderTarget(); |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 44 | rt->prepareForExternalIO(); |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 45 | return rt; |
| 46 | } |
| 47 | |
| 48 | GrBackendObject SkSurface_Gpu::onGetTextureHandle(BackendHandleAccess access) { |
| 49 | GrRenderTarget* rt = prepare_rt_for_external_access(this, access); |
| 50 | GrTexture* texture = rt->asTexture(); |
| 51 | if (texture) { |
| 52 | return texture->getTextureHandle(); |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | bool SkSurface_Gpu::onGetRenderTargetHandle(GrBackendObject* obj, BackendHandleAccess access) { |
| 58 | GrRenderTarget* rt = prepare_rt_for_external_access(this, access); |
| 59 | *obj = rt->getRenderTargetHandle(); |
| 60 | return true; |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 61 | } |
| 62 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 63 | SkCanvas* SkSurface_Gpu::onNewCanvas() { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 64 | SkCanvas::InitFlags flags = SkCanvas::kDefault_InitFlags; |
| 65 | // When we think this works... |
| 66 | // flags |= SkCanvas::kConservativeRasterClip_InitFlag; |
| 67 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 68 | return new SkCanvas(fDevice, flags); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 71 | SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 72 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 73 | int sampleCount = rt->numColorSamples(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 74 | // TODO: Make caller specify this (change virtual signature of onNewSurface). |
| 75 | static const Budgeted kBudgeted = kNo_Budgeted; |
| 76 | return SkSurface::NewRenderTarget(fDevice->context(), kBudgeted, info, sampleCount, |
| 77 | &this->props()); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 80 | SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 81 | const SkImageInfo info = fDevice->imageInfo(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 82 | SkImage* image = nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 83 | GrTexture* tex = fDevice->accessRenderTarget()->asTexture(); |
| 84 | if (tex) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 85 | image = new SkImage_Gpu(info.width(), info.height(), kNeedNewImageUniqueID, |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 86 | info.alphaType(), tex, budgeted); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 87 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 88 | return image; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 91 | // Create a new render target and, if necessary, copy the contents of the old |
| 92 | // render target into it. Note that this flushes the SkGpuDevice but |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 93 | // doesn't force an OpenGL flush. |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 94 | void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 95 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 96 | // are we sharing our render target with the image? Note this call should never create a new |
| 97 | // image because onCopyOnWrite is only called when there is a cached image. |
| 98 | SkImage* image = this->getCachedImage(kNo_Budgeted); |
| 99 | SkASSERT(image); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 100 | if (rt->asTexture() == as_IB(image)->getTexture()) { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 101 | this->fDevice->replaceRenderTarget(SkSurface::kRetain_ContentChangeMode == mode); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 102 | SkTextureImageApplyBudgetedDecision(image); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 103 | } else if (kDiscard_ContentChangeMode == mode) { |
| 104 | this->SkSurface_Gpu::onDiscard(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 105 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 106 | } |
| 107 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 108 | void SkSurface_Gpu::onDiscard() { |
| 109 | fDevice->accessRenderTarget()->discard(); |
| 110 | } |
| 111 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 114 | SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurfaceProps* props) { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 115 | SkAutoTUnref<SkGpuDevice> device( |
| 116 | SkGpuDevice::Create(target, props, SkGpuDevice::kUninit_InitContents)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 117 | if (!device) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | return nullptr; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 119 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 120 | return new SkSurface_Gpu(device); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 121 | } |
| 122 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 123 | SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, Budgeted budgeted, const SkImageInfo& info, |
jcgregorio | e7d7f90 | 2016-02-04 19:51:25 -0800 | [diff] [blame] | 124 | int sampleCount, const SkSurfaceProps* props) { |
| 125 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(ctx, budgeted, info, sampleCount, props, |
| 126 | SkGpuDevice::kClear_InitContents)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 127 | if (!device) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | return nullptr; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 129 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 130 | return new SkSurface_Gpu(device); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 131 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 132 | |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame] | 133 | SkSurface* SkSurface::NewFromBackendTexture(GrContext* context, const GrBackendTextureDesc& desc, |
| 134 | const SkSurfaceProps* props) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | if (nullptr == context) { |
| 136 | return nullptr; |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 137 | } |
| 138 | if (!SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | return nullptr; |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 140 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 141 | SkAutoTUnref<GrSurface> surface(context->textureProvider()->wrapBackendTexture(desc, |
| 142 | kBorrow_GrWrapOwnership)); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 143 | if (!surface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 144 | return nullptr; |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 145 | } |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 146 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(surface->asRenderTarget(), props, |
| 147 | SkGpuDevice::kUninit_InitContents)); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 148 | if (!device) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | return nullptr; |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 150 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 151 | return new SkSurface_Gpu(device); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 152 | } |
| 153 | |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame] | 154 | SkSurface* SkSurface::NewFromBackendRenderTarget(GrContext* context, |
| 155 | const GrBackendRenderTargetDesc& desc, |
| 156 | const SkSurfaceProps* props) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 157 | if (nullptr == context) { |
| 158 | return nullptr; |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame] | 159 | } |
| 160 | SkAutoTUnref<GrRenderTarget> rt(context->textureProvider()->wrapBackendRenderTarget(desc)); |
| 161 | if (!rt) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | return nullptr; |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame] | 163 | } |
| 164 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(rt, props, |
| 165 | SkGpuDevice::kUninit_InitContents)); |
| 166 | if (!device) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | return nullptr; |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame] | 168 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 169 | return new SkSurface_Gpu(device); |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame] | 170 | } |
| 171 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 172 | #endif |