1 /*
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
19 #include "TypeCast.h"
20 #include "GraphicsJNI.h"
21
22 #include <hwui/Canvas.h>
23 #include <utils/Color.h>
24
25 #include <SkBitmap.h>
26 #include <SkColorSpace.h>
27 #include <SkSurface.h>
28 #include <SkRefCnt.h>
29
30 using namespace android;
31
32 /*
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 */
convert(const ANativeWindow_Buffer * buffer,int32_t dataspace,SkBitmap * outBitmap)37 static 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 SkSurfaces::WrapPixels fails then we should as well as we will not be able to
49 // draw into the canvas.
50 sk_sp<SkSurface> surface = SkSurfaces::WrapPixels(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
ACanvas_isSupportedPixelFormat(int32_t bufferFormat)61 bool ACanvas_isSupportedPixelFormat(int32_t bufferFormat) {
62 char pixels[8];
63 ANativeWindow_Buffer buffer { 1, 1, 1, bufferFormat, pixels, {0} };
64 return convert(&buffer, HAL_DATASPACE_UNKNOWN, nullptr);
65 }
66
ACanvas_getNativeHandleFromJava(JNIEnv * env,jobject canvasObj)67 ACanvas* ACanvas_getNativeHandleFromJava(JNIEnv* env, jobject canvasObj) {
68 return TypeCast::toACanvas(GraphicsJNI::getNativeCanvas(env, canvasObj));
69 }
70
ACanvas_createCanvas(const ANativeWindow_Buffer * buffer,int32_t dataspace)71 ACanvas* ACanvas_createCanvas(const ANativeWindow_Buffer* buffer,
72 int32_t /*android_dataspace_t*/ dataspace) {
73 SkBitmap bitmap;
74 bool isValidBuffer = convert(buffer, dataspace, &bitmap);
75 return isValidBuffer ? TypeCast::toACanvas(Canvas::create_canvas(bitmap)) : nullptr;
76 }
77
ACanvas_destroyCanvas(ACanvas * canvas)78 void ACanvas_destroyCanvas(ACanvas* canvas) {
79 delete TypeCast::toCanvas(canvas);
80 }
81
ACanvas_setBuffer(ACanvas * canvas,const ANativeWindow_Buffer * buffer,int32_t dataspace)82 bool ACanvas_setBuffer(ACanvas* canvas, const ANativeWindow_Buffer* buffer,
83 int32_t /*android_dataspace_t*/ dataspace) {
84 SkBitmap bitmap;
85 bool isValidBuffer = (buffer == nullptr) ? false : convert(buffer, dataspace, &bitmap);
86 TypeCast::toCanvas(canvas)->setBitmap(bitmap);
87 return isValidBuffer;
88 }
89
ACanvas_clipRect(ACanvas * canvas,const ARect * clipRect,bool)90 void ACanvas_clipRect(ACanvas* canvas, const ARect* clipRect, bool /*doAA*/) {
91 //TODO update Canvas to take antialias param
92 TypeCast::toCanvas(canvas)->clipRect(clipRect->left, clipRect->top, clipRect->right,
93 clipRect->bottom, SkClipOp::kIntersect);
94 }
95
ACanvas_clipOutRect(ACanvas * canvas,const ARect * clipRect,bool)96 void 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
ACanvas_drawRect(ACanvas * canvas,const ARect * rect,const APaint * paint)102 void 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
ACanvas_drawBitmap(ACanvas * canvas,const ABitmap * bitmap,float left,float top,const APaint * paint)107 void 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));
111 }
112