blob: 7ccff17d9b09fd6946685b81dce25e865c966245 [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
bsalomoneaaaf0b2015-01-23 08:08:04 -08008#include "SkImage_Gpu.h"
reed@google.com5d4ba882012-07-31 15:45:27 +00009#include "SkCanvas.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000010#include "GrContext.h"
reed@google.com5d4ba882012-07-31 15:45:27 +000011
bsalomoneaaaf0b2015-01-23 08:08:04 -080012SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap, int sampleCountForNewSurfaces,
13 SkSurface::Budgeted budgeted)
reed4af267b2014-11-21 08:46:37 -080014 : INHERITED(bitmap.width(), bitmap.height(), NULL)
15 , fBitmap(bitmap)
reed3f10b9d2014-11-21 10:27:53 -080016 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces)
bsalomoneaaaf0b2015-01-23 08:08:04 -080017 , fBudgeted(budgeted)
reed4af267b2014-11-21 08:46:37 -080018{
bsalomon49f085d2014-09-05 13:34:00 -070019 SkASSERT(fBitmap.getTexture());
reed@google.com5d4ba882012-07-31 15:45:27 +000020}
21
piotaixr76d5b472014-07-22 15:02:05 -070022SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX,
23 SkShader::TileMode tileY,
24 const SkMatrix* localMatrix) const
25{
26 return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070027}
28
reed8572fc02014-08-11 13:03:55 -070029void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
reed@google.com5d4ba882012-07-31 15:45:27 +000030 canvas->drawBitmap(fBitmap, x, y, paint);
31}
32
piotaixr5ceff912014-09-26 07:36:26 -070033void SkImage_Gpu::onDrawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
reed8572fc02014-08-11 13:03:55 -070034 const SkPaint* paint) const {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000035 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
36}
37
reed4af267b2014-11-21 08:46:37 -080038SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
39 GrContext* ctx = this->getTexture()->getContext();
bsalomonafe30052015-01-16 07:32:33 -080040 // TODO: Change signature of onNewSurface to take a budgeted param.
41 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
42 return SkSurface::NewRenderTarget(ctx, kBudgeted, info, fSampleCountForNewSurfaces, &props);
reed4af267b2014-11-21 08:46:37 -080043}
44
reed8572fc02014-08-11 13:03:55 -070045GrTexture* SkImage_Gpu::onGetTexture() const {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000046 return fBitmap.getTexture();
reed@google.com5d4ba882012-07-31 15:45:27 +000047}
48
reed@google.comace13542014-02-06 22:00:58 +000049bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000050 return fBitmap.copyTo(dst, kN32_SkColorType);
reed@google.comace13542014-02-06 22:00:58 +000051}
52
piotaixrd2a35222014-08-19 14:29:02 -070053bool SkImage_Gpu::isOpaque() const {
54 return fBitmap.isOpaque();
55}
56
robertphillips@google.com97b6b072012-10-31 14:48:39 +000057///////////////////////////////////////////////////////////////////////////////
58
bsalomoneaaaf0b2015-01-23 08:08:04 -080059SkImage* SkNewImageFromBitmapTexture(const SkBitmap& bitmap, int sampleCountForNewSurfaces,
60 SkSurface::Budgeted budgeted) {
reed7983bf92015-01-23 04:24:16 -080061 if (0 == bitmap.width() || 0 == bitmap.height() || NULL == bitmap.getTexture()) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000062 return NULL;
63 }
bsalomoneaaaf0b2015-01-23 08:08:04 -080064 return SkNEW_ARGS(SkImage_Gpu, (bitmap, sampleCountForNewSurfaces, budgeted));
reed4af267b2014-11-21 08:46:37 -080065}
robertphillips@google.com97b6b072012-10-31 14:48:39 +000066
robertphillips@google.com97b6b072012-10-31 14:48:39 +000067GrTexture* SkTextureImageGetTexture(SkImage* image) {
68 return ((SkImage_Gpu*)image)->getTexture();
69}
reed4af267b2014-11-21 08:46:37 -080070
bsalomoneaaaf0b2015-01-23 08:08:04 -080071extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
72 ((SkImage_Gpu*)image)->applyBudgetDecision();
73}