blob: e673cef6703aecefbeb8acbdd2b78164247f4f52 [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
8#include "SkSurface_Base.h"
9#include "SkImagePriv.h"
10#include "SkCanvas.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000011#include "SkGpuDevice.h"
reed@google.com5d4ba882012-07-31 15:45:27 +000012
13class SkSurface_Gpu : public SkSurface_Base {
14public:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015 SK_DECLARE_INST_COUNT(SkSurface_Gpu)
reed@google.com5d4ba882012-07-31 15:45:27 +000016
reed@google.com2bd8b812013-11-01 13:46:54 +000017 SkSurface_Gpu(GrContext*, const SkImageInfo&, int sampleCount);
robertphillips@google.com97b6b072012-10-31 14:48:39 +000018 SkSurface_Gpu(GrContext*, GrRenderTarget*);
19 virtual ~SkSurface_Gpu();
reed@google.com5d4ba882012-07-31 15:45:27 +000020
21 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
reed@google.com2bd8b812013-11-01 13:46:54 +000022 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
junov@chromium.org5ee449a2013-04-12 20:20:50 +000023 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
reed@google.com5d4ba882012-07-31 15:45:27 +000024 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
25 const SkPaint*) SK_OVERRIDE;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000026 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE;
reed@google.com5d4ba882012-07-31 15:45:27 +000027
28private:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000029 SkGpuDevice* fDevice;
reed@google.com5d4ba882012-07-31 15:45:27 +000030
31 typedef SkSurface_Base INHERITED;
32};
33
34///////////////////////////////////////////////////////////////////////////////
35
reed@google.com2bd8b812013-11-01 13:46:54 +000036SkSurface_Gpu::SkSurface_Gpu(GrContext* ctx, const SkImageInfo& info,
robertphillips@google.com97b6b072012-10-31 14:48:39 +000037 int sampleCount)
reed@google.com5d4ba882012-07-31 15:45:27 +000038 : INHERITED(info.fWidth, info.fHeight) {
reed@google.com383a6972013-10-21 14:00:07 +000039 SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040
robertphillips@google.com97b6b072012-10-31 14:48:39 +000041 fDevice = SkNEW_ARGS(SkGpuDevice, (ctx, config, info.fWidth, info.fHeight, sampleCount));
robertphillips@google.comea5d8af2012-11-02 17:38:28 +000042
reed@google.com383a6972013-10-21 14:00:07 +000043 if (!SkAlphaTypeIsOpaque(info.fAlphaType)) {
robertphillips@google.comea5d8af2012-11-02 17:38:28 +000044 fDevice->clear(0x0);
45 }
reed@google.com5d4ba882012-07-31 15:45:27 +000046}
47
robertphillips@google.com97b6b072012-10-31 14:48:39 +000048SkSurface_Gpu::SkSurface_Gpu(GrContext* ctx, GrRenderTarget* renderTarget)
49 : INHERITED(renderTarget->width(), renderTarget->height()) {
50 fDevice = SkNEW_ARGS(SkGpuDevice, (ctx, renderTarget));
robertphillips@google.comea5d8af2012-11-02 17:38:28 +000051
52 if (kRGB_565_GrPixelConfig != renderTarget->config()) {
53 fDevice->clear(0x0);
54 }
robertphillips@google.com97b6b072012-10-31 14:48:39 +000055}
reed@google.com5d4ba882012-07-31 15:45:27 +000056
robertphillips@google.com97b6b072012-10-31 14:48:39 +000057SkSurface_Gpu::~SkSurface_Gpu() {
58 SkSafeUnref(fDevice);
reed@google.com5d4ba882012-07-31 15:45:27 +000059}
60
61SkCanvas* SkSurface_Gpu::onNewCanvas() {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000062 return SkNEW_ARGS(SkCanvas, (fDevice));
reed@google.com5d4ba882012-07-31 15:45:27 +000063}
64
reed@google.com2bd8b812013-11-01 13:46:54 +000065SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +000066 GrRenderTarget* rt = fDevice->accessRenderTarget();
robertphillips@google.com97b6b072012-10-31 14:48:39 +000067 int sampleCount = rt->numSamples();
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000068 return SkSurface::NewRenderTarget(fDevice->context(), info, sampleCount);
reed@google.com5d4ba882012-07-31 15:45:27 +000069}
70
junov@chromium.org5ee449a2013-04-12 20:20:50 +000071SkImage* SkSurface_Gpu::onNewImageSnapshot() {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000072 return SkImage::NewTexture(fDevice->accessBitmap(false));
reed@google.com5d4ba882012-07-31 15:45:27 +000073}
74
75void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
76 const SkPaint* paint) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000077 canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint);
78}
79
junov@chromium.org45c3db82013-04-11 17:52:05 +000080// Create a new SkGpuDevice and, if necessary, copy the contents of the old
81// device into it. Note that this flushes the SkGpuDevice but
robertphillips@google.com97b6b072012-10-31 14:48:39 +000082// doesn't force an OpenGL flush.
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000083void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +000084 GrRenderTarget* rt = fDevice->accessRenderTarget();
robertphillips@google.com97b6b072012-10-31 14:48:39 +000085 // are we sharing our render target with the image?
junov@chromium.orgacea3ef2013-04-16 19:41:09 +000086 SkASSERT(NULL != this->getCachedImage());
87 if (rt->asTexture() == SkTextureImageGetTexture(this->getCachedImage())) {
junov@chromium.org45c3db82013-04-11 17:52:05 +000088 SkGpuDevice* newDevice = static_cast<SkGpuDevice*>(
89 fDevice->createCompatibleDevice(fDevice->config(), fDevice->width(),
90 fDevice->height(), fDevice->isOpaque()));
91 SkAutoTUnref<SkGpuDevice> aurd(newDevice);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000092 if (kRetain_ContentChangeMode == mode) {
93 fDevice->context()->copyTexture(rt->asTexture(),
94 reinterpret_cast<GrRenderTarget*>(newDevice->accessRenderTarget()));
95 }
junov@chromium.orgacea3ef2013-04-16 19:41:09 +000096 SkASSERT(NULL != this->getCachedCanvas());
97 SkASSERT(this->getCachedCanvas()->getDevice() == fDevice);
98 this->getCachedCanvas()->setDevice(newDevice);
junov@chromium.org45c3db82013-04-11 17:52:05 +000099 SkRefCnt_SafeAssign(fDevice, newDevice);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000100 }
reed@google.com5d4ba882012-07-31 15:45:27 +0000101}
102
103///////////////////////////////////////////////////////////////////////////////
104
105SkSurface* SkSurface::NewRenderTargetDirect(GrContext* ctx,
106 GrRenderTarget* target) {
107 if (NULL == ctx || NULL == target) {
108 return NULL;
109 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000110
reed@google.com5d4ba882012-07-31 15:45:27 +0000111 return SkNEW_ARGS(SkSurface_Gpu, (ctx, target));
112}
113
reed@google.com2bd8b812013-11-01 13:46:54 +0000114SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImageInfo& info, int sampleCount) {
reed@google.com5d4ba882012-07-31 15:45:27 +0000115 if (NULL == ctx) {
116 return NULL;
117 }
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000118
reed@google.com383a6972013-10-21 14:00:07 +0000119 SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000120
121 GrTextureDesc desc;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000122 desc.fFlags = kRenderTarget_GrTextureFlagBit | kCheckAllocation_GrTextureFlagBit;
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000123 desc.fWidth = info.fWidth;
124 desc.fHeight = info.fHeight;
125 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
126 desc.fSampleCnt = sampleCount;
127
robertphillips@google.com671eac62012-11-01 17:31:02 +0000128 SkAutoTUnref<GrTexture> tex(ctx->createUncachedTexture(desc, NULL, 0));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000129 if (NULL == tex) {
reed@google.com5d4ba882012-07-31 15:45:27 +0000130 return NULL;
131 }
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000132
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000133 return SkNEW_ARGS(SkSurface_Gpu, (ctx, tex->asRenderTarget()));
reed@google.com5d4ba882012-07-31 15:45:27 +0000134}