blob: 2a939efed9bb409145e56f00b22c1909faf18723 [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>
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040026#include <SkSurface.h>
Derek Sollenbergerc287a772019-08-02 13:44:31 -040027
28using namespace android;
29
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040030/*
31 * Converts a buffer and dataspace into an SkBitmap only if the resulting bitmap can be treated as a
32 * rendering destination for a Canvas. If the buffer is null or the format is one that we cannot
33 * render into with a Canvas then false is returned and the outBitmap param is unmodified.
34 */
35static bool convert(const ANativeWindow_Buffer* buffer,
36 int32_t /*android_dataspace_t*/ dataspace,
37 SkBitmap* outBitmap) {
38 if (buffer == nullptr) {
39 return false;
40 }
41
42 sk_sp<SkColorSpace> cs(uirenderer::DataSpaceToColorSpace((android_dataspace)dataspace));
43 SkImageInfo imageInfo = uirenderer::ANativeWindowToImageInfo(*buffer, cs);
44 size_t rowBytes = buffer->stride * imageInfo.bytesPerPixel();
45
46 // If SkSurface::MakeRasterDirect fails then we should as well as we will not be able to
47 // draw into the canvas.
48 sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(imageInfo, buffer->bits, rowBytes);
49 if (surface.get() != nullptr) {
50 if (outBitmap) {
51 outBitmap->setInfo(imageInfo, rowBytes);
52 outBitmap->setPixels(buffer->bits);
53 }
54 return true;
55 }
56 return false;
57}
58
Derek Sollenbergerc287a772019-08-02 13:44:31 -040059bool ACanvas_isSupportedPixelFormat(int32_t bufferFormat) {
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040060 char pixels[8];
61 ANativeWindow_Buffer buffer { 1, 1, 1, bufferFormat, pixels, {0} };
62 return convert(&buffer, HAL_DATASPACE_UNKNOWN, nullptr);
Derek Sollenbergerc287a772019-08-02 13:44:31 -040063}
64
65ACanvas* ACanvas_getNativeHandleFromJava(JNIEnv* env, jobject canvasObj) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040066 return TypeCast::toACanvas(GraphicsJNI::getNativeCanvas(env, canvasObj));
Derek Sollenbergerc287a772019-08-02 13:44:31 -040067}
68
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040069ACanvas* ACanvas_createCanvas(const ANativeWindow_Buffer* buffer,
70 int32_t /*android_dataspace_t*/ dataspace) {
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040071 SkBitmap bitmap;
72 bool isValidBuffer = convert(buffer, dataspace, &bitmap);
73 return isValidBuffer ? TypeCast::toACanvas(Canvas::create_canvas(bitmap)) : nullptr;
Derek Sollenbergerc287a772019-08-02 13:44:31 -040074}
75
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040076void ACanvas_destroyCanvas(ACanvas* canvas) {
77 delete TypeCast::toCanvas(canvas);
78}
79
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040080bool ACanvas_setBuffer(ACanvas* canvas, const ANativeWindow_Buffer* buffer,
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040081 int32_t /*android_dataspace_t*/ dataspace) {
Derek Sollenberger4aa30d02019-08-30 13:53:29 -040082 SkBitmap bitmap;
83 bool isValidBuffer = (buffer == nullptr) ? false : convert(buffer, dataspace, &bitmap);
84 TypeCast::toCanvas(canvas)->setBitmap(bitmap);
85 return isValidBuffer;
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040086}
87
88void ACanvas_clipRect(ACanvas* canvas, const ARect* clipRect, bool /*doAA*/) {
Derek Sollenbergerc287a772019-08-02 13:44:31 -040089 //TODO update Canvas to take antialias param
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040090 TypeCast::toCanvas(canvas)->clipRect(clipRect->left, clipRect->top, clipRect->right,
91 clipRect->bottom, SkClipOp::kIntersect);
92}
93
94void ACanvas_clipOutRect(ACanvas* canvas, const ARect* clipRect, bool /*doAA*/) {
95 //TODO update Canvas to take antialias param
96 TypeCast::toCanvas(canvas)->clipRect(clipRect->left, clipRect->top, clipRect->right,
97 clipRect->bottom, SkClipOp::kDifference);
98}
99
100void ACanvas_drawRect(ACanvas* canvas, const ARect* rect, const APaint* paint) {
101 TypeCast::toCanvas(canvas)->drawRect(rect->left, rect->top, rect->right, rect->bottom,
102 TypeCast::toPaintRef(paint));
103}
104
105void ACanvas_drawBitmap(ACanvas* canvas, const ABitmap* bitmap, float left, float top,
106 const APaint* paint) {
107 TypeCast::toCanvas(canvas)->drawBitmap(TypeCast::toBitmapRef(bitmap), left, top,
108 TypeCast::toPaint(paint));
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400109}