blob: fa5daec2696b34d6f6ae4b5570ca2c472e8e9f95 [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +00001/*
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
bsalomonafe30052015-01-16 07:32:33 -08008#include "SkSurface_Gpu.h"
9
reed@google.com5d4ba882012-07-31 15:45:27 +000010#include "SkCanvas.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000011#include "SkGpuDevice.h"
bsalomonafe30052015-01-16 07:32:33 -080012#include "SkImage_Base.h"
reed8b26b992015-05-07 15:36:17 -070013#include "SkImage_Gpu.h"
bsalomonafe30052015-01-16 07:32:33 -080014#include "SkImagePriv.h"
15#include "SkSurface_Base.h"
reed@google.com5d4ba882012-07-31 15:45:27 +000016
reedf037e0b2014-10-30 11:34:15 -070017#if SK_SUPPORT_GPU
18
reed@google.com5d4ba882012-07-31 15:45:27 +000019///////////////////////////////////////////////////////////////////////////////
20
bsalomonafe30052015-01-16 07:32:33 -080021SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device)
22 : INHERITED(device->width(), device->height(), &device->surfaceProps())
23 , fDevice(SkRef(device)) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000024}
reed@google.com5d4ba882012-07-31 15:45:27 +000025
robertphillips@google.com97b6b072012-10-31 14:48:39 +000026SkSurface_Gpu::~SkSurface_Gpu() {
kkinnunenabcfab42015-02-22 22:53:44 -080027 fDevice->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000028}
29
30SkCanvas* SkSurface_Gpu::onNewCanvas() {
reed4a8126e2014-09-22 07:29:03 -070031 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.com5d4ba882012-07-31 15:45:27 +000036}
37
reed@google.com2bd8b812013-11-01 13:46:54 +000038SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +000039 GrRenderTarget* rt = fDevice->accessRenderTarget();
robertphillips@google.com97b6b072012-10-31 14:48:39 +000040 int sampleCount = rt->numSamples();
bsalomonafe30052015-01-16 07:32:33 -080041 // 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.com5d4ba882012-07-31 15:45:27 +000045}
46
bsalomoneaaaf0b2015-01-23 08:08:04 -080047SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) {
reed8b26b992015-05-07 15:36:17 -070048 const SkImageInfo info = fDevice->imageInfo();
reed4af267b2014-11-21 08:46:37 -080049 const int sampleCount = fDevice->accessRenderTarget()->numSamples();
reed8b26b992015-05-07 15:36:17 -070050 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 }
reed4af267b2014-11-21 08:46:37 -080057 if (image) {
58 as_IB(image)->initWithProps(this->props());
59 }
60 return image;
reed@google.com5d4ba882012-07-31 15:45:27 +000061}
62
63void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
64 const SkPaint* paint) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000065 canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint);
66}
67
kkinnunenabcfab42015-02-22 22:53:44 -080068// 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.com97b6b072012-10-31 14:48:39 +000070// doesn't force an OpenGL flush.
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000071void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +000072 GrRenderTarget* rt = fDevice->accessRenderTarget();
bsalomoneaaaf0b2015-01-23 08:08:04 -080073 // 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);
reed8b26b992015-05-07 15:36:17 -070077 if (rt->asTexture() == image->getTexture()) {
kkinnunenabcfab42015-02-22 22:53:44 -080078 this->fDevice->replaceRenderTarget(SkSurface::kRetain_ContentChangeMode == mode);
bsalomoneaaaf0b2015-01-23 08:08:04 -080079 SkTextureImageApplyBudgetedDecision(image);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000080 } else if (kDiscard_ContentChangeMode == mode) {
81 this->SkSurface_Gpu::onDiscard();
robertphillips@google.com97b6b072012-10-31 14:48:39 +000082 }
reed@google.com5d4ba882012-07-31 15:45:27 +000083}
84
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000085void SkSurface_Gpu::onDiscard() {
86 fDevice->accessRenderTarget()->discard();
87}
88
reed@google.com5d4ba882012-07-31 15:45:27 +000089///////////////////////////////////////////////////////////////////////////////
90
reed4a8126e2014-09-22 07:29:03 -070091SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurfaceProps* props) {
bsalomonafe30052015-01-16 07:32:33 -080092 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target, props));
93 if (!device) {
reed@google.com5d4ba882012-07-31 15:45:27 +000094 return NULL;
95 }
bsalomonafe30052015-01-16 07:32:33 -080096 return SkNEW_ARGS(SkSurface_Gpu, (device));
reed@google.com5d4ba882012-07-31 15:45:27 +000097}
98
bsalomonafe30052015-01-16 07:32:33 -080099SkSurface* 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.com5d4ba882012-07-31 15:45:27 +0000104 return NULL;
105 }
bsalomonafe30052015-01-16 07:32:33 -0800106 return SkNEW_ARGS(SkSurface_Gpu, (device));
reed@google.com5d4ba882012-07-31 15:45:27 +0000107}
reedf037e0b2014-10-30 11:34:15 -0700108
bsalomone4579ad2015-04-08 08:38:40 -0700109SkSurface* 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 }
bsalomond309e7a2015-04-30 14:18:54 -0700117 SkAutoTUnref<GrSurface> surface(context->textureProvider()->wrapBackendTexture(desc));
bsalomone4579ad2015-04-08 08:38:40 -0700118 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
reedf037e0b2014-10-30 11:34:15 -0700129#endif