blob: 204f38d65b9d38aefb1251793662583af88f3c6e [file] [log] [blame]
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001/*
2 * Copyright 2013 The Android Open Source Project
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
robertphillips@google.comc3ecf652013-01-31 02:33:09 +00008#include "SkMatrix.h"
9
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000010#if SK_SUPPORT_GPU
11#include "GrTexture.h"
12#include "SkImageFilterUtils.h"
13#include "SkBitmap.h"
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000014#include "SkGrPixelRef.h"
15#include "SkGr.h"
16
17bool SkImageFilterUtils::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000018 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orge13af712014-01-13 20:39:14 +000019 result->setConfig(info);
20 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000021 return true;
22}
23
commit-bot@chromium.org7b320702013-07-10 21:22:18 +000024bool SkImageFilterUtils::GetInputResultGPU(SkImageFilter* filter, SkImageFilter::Proxy* proxy,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +000025 const SkBitmap& src, const SkMatrix& ctm,
26 SkBitmap* result, SkIPoint* offset) {
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +000027 // Ensure that GrContext calls under filterImage and filterImageGPU below will see an identity
28 // matrix with no clip and that the matrix, clip, and render target set before this function was
29 // called are restored before we return to the caller.
30 GrContext* context = src.getTexture()->getContext();
31 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000032 if (!filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000033 offset->fX = offset->fY = 0;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000034 *result = src;
35 return true;
36 } else if (filter->canFilterImageGPU()) {
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +000037 return filter->filterImageGPU(proxy, src, ctm, result, offset);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000038 } else {
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +000039 if (filter->filterImage(proxy, src, ctm, result, offset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000040 if (!result->getTexture()) {
reed@google.combf790232013-12-13 19:45:58 +000041 SkImageInfo info;
42 if (!result->asImageInfo(&info)) {
43 return false;
44 }
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +000045 GrTexture* resultTex = GrLockAndRefCachedBitmapTexture(context, *result, NULL);
reed@google.combf790232013-12-13 19:45:58 +000046 result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000047 GrUnlockAndUnrefCachedBitmapTexture(resultTex);
48 }
49 return true;
50 } else {
51 return false;
52 }
53 }
54}
55#endif