blob: 8977d3ce4da33d9e37d2a8e160eeec8afddd1407 [file] [log] [blame]
John Reck115195e2023-02-01 20:57:44 -05001/*
2 * Copyright (C) 2023 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 "GainmapRenderer.h"
18
19#include <SkGainmapShader.h>
20
21#include "Gainmap.h"
22#include "Rect.h"
23#include "utils/Trace.h"
24
25#ifdef __ANDROID__
26#include "renderthread/CanvasContext.h"
27#endif
28
29namespace android::uirenderer {
30
31using namespace renderthread;
32
33void DrawGainmapBitmap(SkCanvas* c, const sk_sp<const SkImage>& image, const SkRect& src,
34 const SkRect& dst, const SkSamplingOptions& sampling, const SkPaint* paint,
35 SkCanvas::SrcRectConstraint constraint,
36 const sk_sp<const SkImage>& gainmapImage, const SkGainmapInfo& gainmapInfo) {
37 ATRACE_CALL();
38#ifdef __ANDROID__
39 CanvasContext* context = CanvasContext::getActiveContext();
40 float targetSdrHdrRatio = context ? context->targetSdrHdrRatio() : 1.f;
41 if (targetSdrHdrRatio > 1.f && gainmapImage) {
42 SkPaint gainmapPaint = *paint;
43 float sX = gainmapImage->width() / (float)image->width();
44 float sY = gainmapImage->height() / (float)image->height();
45 SkRect gainmapSrc = src;
46 // TODO: Tweak rounding?
47 gainmapSrc.fLeft *= sX;
48 gainmapSrc.fRight *= sX;
49 gainmapSrc.fTop *= sY;
50 gainmapSrc.fBottom *= sY;
John Reck9f85cd32023-02-14 19:40:20 -050051 auto shader = SkGainmapShader::Make(image, src, sampling, gainmapImage, gainmapSrc,
John Reck115195e2023-02-01 20:57:44 -050052 sampling, gainmapInfo, dst, targetSdrHdrRatio,
53 c->imageInfo().refColorSpace());
54 gainmapPaint.setShader(shader);
55 c->drawRect(dst, gainmapPaint);
56 } else
57#endif
58 c->drawImageRect(image.get(), src, dst, sampling, paint, constraint);
59}
60
61} // namespace android::uirenderer