Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "AutoBackendTexture.h" |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "RenderEngine" |
| 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 23 | #include <include/core/SkImage.h> |
| 24 | #include <include/core/SkSurface.h> |
| 25 | |
| 26 | #include "compat/SkiaBackendTexture.h" |
| 27 | |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 28 | #include <common/trace.h> |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 29 | #include <log/log_main.h> |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace renderengine { |
| 33 | namespace skia { |
| 34 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 35 | AutoBackendTexture::AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture, |
| 36 | CleanupManager& cleanupMgr) |
| 37 | : mCleanupMgr(cleanupMgr), mBackendTexture(std::move(backendTexture)) {} |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 38 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 39 | void AutoBackendTexture::unref(bool releaseLocalResources) { |
| 40 | if (releaseLocalResources) { |
| 41 | mSurface = nullptr; |
| 42 | mImage = nullptr; |
| 43 | } |
| 44 | |
| 45 | mUsageCount--; |
| 46 | if (mUsageCount <= 0) { |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 47 | mCleanupMgr.add(this); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | // releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use. |
| 52 | // "releaseContext" contains an "AutoBackendTexture*". |
| 53 | void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) { |
| 54 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); |
| 55 | textureRelease->unref(false); |
| 56 | } |
| 57 | |
| 58 | // releaseImageProc is invoked by SkImage, when the texture is no longer in use. |
| 59 | // "releaseContext" contains an "AutoBackendTexture*". |
Kevin Lubick | 95afb8a | 2023-05-11 14:57:41 +0000 | [diff] [blame] | 60 | void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) { |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 61 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); |
| 62 | textureRelease->unref(false); |
| 63 | } |
| 64 | |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 65 | sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 66 | SFTRACE_CALL(); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 67 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 68 | sk_sp<SkImage> image = mBackendTexture->makeImage(alphaType, dataspace, releaseImageProc, this); |
| 69 | // The following ref will be counteracted by releaseProc, when SkImage is discarded. |
| 70 | ref(); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 71 | |
| 72 | mImage = image; |
| 73 | mDataspace = dataspace; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 74 | return mImage; |
| 75 | } |
| 76 | |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 77 | sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace) { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 78 | SFTRACE_CALL(); |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 79 | LOG_ALWAYS_FATAL_IF(!mBackendTexture->isOutputBuffer(), |
| 80 | "You can't generate an SkSurface for a read-only texture"); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 81 | if (!mSurface.get() || mDataspace != dataspace) { |
| 82 | sk_sp<SkSurface> surface = |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 83 | mBackendTexture->makeSurface(dataspace, releaseSurfaceProc, this); |
| 84 | // The following ref will be counteracted by releaseProc, when SkSurface is discarded. |
| 85 | ref(); |
| 86 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 87 | mSurface = surface; |
| 88 | } |
| 89 | |
| 90 | mDataspace = dataspace; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 91 | return mSurface; |
| 92 | } |
| 93 | |
| 94 | } // namespace skia |
| 95 | } // namespace renderengine |
Alec Mouri | c6709cc | 2021-04-22 17:59:00 -0700 | [diff] [blame] | 96 | } // namespace android |