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