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 | /////////////////////////////////////////////////////////////////////////////// |
| 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() { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 27 | fDevice->unref(); |
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 | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 48 | const SkImageInfo info = fDevice->imageInfo(); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 49 | const int sampleCount = fDevice->accessRenderTarget()->numSamples(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 50 | SkImage* image = NULL; |
| 51 | GrTexture* tex = fDevice->accessRenderTarget()->asTexture(); |
| 52 | if (tex) { |
| 53 | image = SkNEW_ARGS(SkImage_Gpu, |
| 54 | (info.width(), info.height(), info.alphaType(), |
| 55 | tex, sampleCount, budgeted)); |
| 56 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 57 | if (image) { |
| 58 | as_IB(image)->initWithProps(this->props()); |
| 59 | } |
| 60 | return image; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 64 | const SkPaint* paint) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 65 | canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint); |
| 66 | } |
| 67 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 68 | // Create a new render target and, if necessary, copy the contents of the old |
| 69 | // render target into it. Note that this flushes the SkGpuDevice but |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 70 | // doesn't force an OpenGL flush. |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 71 | void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 72 | GrRenderTarget* rt = fDevice->accessRenderTarget(); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 73 | // are we sharing our render target with the image? Note this call should never create a new |
| 74 | // image because onCopyOnWrite is only called when there is a cached image. |
| 75 | SkImage* image = this->getCachedImage(kNo_Budgeted); |
| 76 | SkASSERT(image); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 77 | if (rt->asTexture() == image->getTexture()) { |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 78 | this->fDevice->replaceRenderTarget(SkSurface::kRetain_ContentChangeMode == mode); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 79 | SkTextureImageApplyBudgetedDecision(image); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 80 | } else if (kDiscard_ContentChangeMode == mode) { |
| 81 | this->SkSurface_Gpu::onDiscard(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 82 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 83 | } |
| 84 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 85 | void SkSurface_Gpu::onDiscard() { |
| 86 | fDevice->accessRenderTarget()->discard(); |
| 87 | } |
| 88 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 91 | SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurfaceProps* props) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 92 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target, props)); |
| 93 | if (!device) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 96 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 99 | SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, Budgeted budgeted, const SkImageInfo& info, |
| 100 | int sampleCount, const SkSurfaceProps* props) { |
| 101 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(ctx, budgeted, info, sampleCount, props, |
| 102 | SkGpuDevice::kNeedClear_Flag)); |
| 103 | if (!device) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 106 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 107 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 108 | |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 109 | SkSurface* SkSurface::NewWrappedRenderTarget(GrContext* context, GrBackendTextureDesc desc, |
| 110 | const SkSurfaceProps* props) { |
| 111 | if (NULL == context) { |
| 112 | return NULL; |
| 113 | } |
| 114 | if (!SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) { |
| 115 | return NULL; |
| 116 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 117 | SkAutoTUnref<GrSurface> surface(context->textureProvider()->wrapBackendTexture(desc)); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 118 | if (!surface) { |
| 119 | return NULL; |
| 120 | } |
| 121 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(surface->asRenderTarget(), props, |
| 122 | SkGpuDevice::kNeedClear_Flag)); |
| 123 | if (!device) { |
| 124 | return NULL; |
| 125 | } |
| 126 | return SkNEW_ARGS(SkSurface_Gpu, (device)); |
| 127 | } |
| 128 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 129 | #endif |