blob: 905b123076a2d37ab158ed0e11561b75cc43a607 [file] [log] [blame]
Derek Sollenbergerc287a772019-08-02 13:44:31 -04001/*
2 * Copyright (C) 2019 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 "android/graphics/canvas.h"
18
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040019#include "TypeCast.h"
Derek Sollenbergerc287a772019-08-02 13:44:31 -040020#include "GraphicsJNI.h"
21
22#include <hwui/Canvas.h>
23#include <utils/Color.h>
24
25#include <SkBitmap.h>
Kevin Lubick07d6aae2022-04-01 14:03:11 -040026#include <SkColorSpace.h>
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040027#include <SkSurface.h>
Kevin Lubick07d6aae2022-04-01 14:03:11 -040028#include <SkRefCnt.h>
Derek Sollenbergerc287a772019-08-02 13:44:31 -040029
30using namespace android;
31
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040032/*
33 * Converts a buffer and dataspace into an SkBitmap only if the resulting bitmap can be treated as a
34 * rendering destination for a Canvas. If the buffer is null or the format is one that we cannot
35 * render into with a Canvas then false is returned and the outBitmap param is unmodified.
36 */
37static bool convert(const ANativeWindow_Buffer* buffer,
38 int32_t /*android_dataspace_t*/ dataspace,
39 SkBitmap* outBitmap) {
40 if (buffer == nullptr) {
41 return false;
42 }
43
44 sk_sp<SkColorSpace> cs(uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace));
45 SkImageInfo imageInfo = uirenderer::ANativeWindowToImageInfo(*buffer, cs);
46 size_t rowBytes = buffer->stride * imageInfo.bytesPerPixel();
47
48 // If SkSurface::MakeRasterDirect fails then we should as well as we will not be able to
49 // draw into the canvas.
50 sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(imageInfo, buffer->bits, rowBytes);
51 if (surface.get() != nullptr) {
52 if (outBitmap) {
53 outBitmap->setInfo(imageInfo, rowBytes);
54 outBitmap->setPixels(buffer->bits);
55 }
56 return true;
57 }
58 return false;
59}
60
Derek Sollenbergerc287a772019-08-02 13:44:31 -040061bool ACanvas_isSupportedPixelFormat(int32_t bufferFormat) {
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040062 char pixels[8];
63 ANativeWindow_Buffer buffer { 1, 1, 1, bufferFormat, pixels, {0} };
64 return convert(&buffer, HAL_DATASPACE_UNKNOWN, nullptr);
Derek Sollenbergerc287a772019-08-02 13:44:31 -040065}
66
67ACanvas* ACanvas_getNativeHandleFromJava(JNIEnv* env, jobject canvasObj) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040068 return TypeCast::toACanvas(GraphicsJNI::getNativeCanvas(env, canvasObj));
Derek Sollenbergerc287a772019-08-02 13:44:31 -040069}
70
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040071ACanvas* ACanvas_createCanvas(const ANativeWindow_Buffer* buffer,
72 int32_t /*android_dataspace_t*/ dataspace) {
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040073 SkBitmap bitmap;
74 bool isValidBuffer = convert(buffer, dataspace, &bitmap);
75 return isValidBuffer ? TypeCast::toACanvas(Canvas::create_canvas(bitmap)) : nullptr;
Derek Sollenbergerc287a772019-08-02 13:44:31 -040076}
77
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040078void ACanvas_destroyCanvas(ACanvas* canvas) {
79 delete TypeCast::toCanvas(canvas);
80}
81
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040082bool ACanvas_setBuffer(ACanvas* canvas, const ANativeWindow_Buffer* buffer,
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040083 int32_t /*android_dataspace_t*/ dataspace) {
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040084 SkBitmap bitmap;
85 bool isValidBuffer = (buffer == nullptr) ? false : convert(buffer, dataspace, &bitmap);
86 TypeCast::toCanvas(canvas)->setBitmap(bitmap);
87 return isValidBuffer;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040088}
89
90void ACanvas_clipRect(ACanvas* canvas, const ARect* clipRect, bool /*doAA*/) {
Derek Sollenbergerc287a772019-08-02 13:44:31 -040091 //TODO update Canvas to take antialias param
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040092 TypeCast::toCanvas(canvas)->clipRect(clipRect->left, clipRect->top, clipRect->right,
93 clipRect->bottom, SkClipOp::kIntersect);
94}
95
96void ACanvas_clipOutRect(ACanvas* canvas, const ARect* clipRect, bool /*doAA*/) {
97 //TODO update Canvas to take antialias param
98 TypeCast::toCanvas(canvas)->clipRect(clipRect->left, clipRect->top, clipRect->right,
99 clipRect->bottom, SkClipOp::kDifference);
100}
101
102void ACanvas_drawRect(ACanvas* canvas, const ARect* rect, const APaint* paint) {
103 TypeCast::toCanvas(canvas)->drawRect(rect->left, rect->top, rect->right, rect->bottom,
104 TypeCast::toPaintRef(paint));
105}
106
107void ACanvas_drawBitmap(ACanvas* canvas, const ABitmap* bitmap, float left, float top,
108 const APaint* paint) {
109 TypeCast::toCanvas(canvas)->drawBitmap(TypeCast::toBitmapRef(bitmap), left, top,
110 TypeCast::toPaint(paint));
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400111}