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 <SkSurface.h>
27 
28 using namespace android;
29 
30 /*
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  */
convert(const ANativeWindow_Buffer * buffer,int32_t dataspace,SkBitmap * outBitmap)35 static 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 
ACanvas_isSupportedPixelFormat(int32_t bufferFormat)59 bool ACanvas_isSupportedPixelFormat(int32_t bufferFormat) {
60     char pixels[8];
61     ANativeWindow_Buffer buffer { 1, 1, 1, bufferFormat, pixels, {0} };
62     return convert(&buffer, HAL_DATASPACE_UNKNOWN, nullptr);
63 }
64 
ACanvas_getNativeHandleFromJava(JNIEnv * env,jobject canvasObj)65 ACanvas* ACanvas_getNativeHandleFromJava(JNIEnv* env, jobject canvasObj) {
66     return TypeCast::toACanvas(GraphicsJNI::getNativeCanvas(env, canvasObj));
67 }
68 
ACanvas_createCanvas(const ANativeWindow_Buffer * buffer,int32_t dataspace)69 ACanvas* ACanvas_createCanvas(const ANativeWindow_Buffer* buffer,
70                               int32_t /*android_dataspace_t*/ dataspace) {
71     SkBitmap bitmap;
72     bool isValidBuffer = convert(buffer, dataspace, &bitmap);
73     return isValidBuffer ? TypeCast::toACanvas(Canvas::create_canvas(bitmap)) : nullptr;
74 }
75 
ACanvas_destroyCanvas(ACanvas * canvas)76 void ACanvas_destroyCanvas(ACanvas* canvas) {
77     delete TypeCast::toCanvas(canvas);
78 }
79 
ACanvas_setBuffer(ACanvas * canvas,const ANativeWindow_Buffer * buffer,int32_t dataspace)80 bool ACanvas_setBuffer(ACanvas* canvas, const ANativeWindow_Buffer* buffer,
81                        int32_t /*android_dataspace_t*/ dataspace) {
82     SkBitmap bitmap;
83     bool isValidBuffer = (buffer == nullptr) ? false : convert(buffer, dataspace, &bitmap);
84     TypeCast::toCanvas(canvas)->setBitmap(bitmap);
85     return isValidBuffer;
86 }
87 
ACanvas_clipRect(ACanvas * canvas,const ARect * clipRect,bool)88 void ACanvas_clipRect(ACanvas* canvas, const ARect* clipRect, bool /*doAA*/) {
89     //TODO update Canvas to take antialias param
90     TypeCast::toCanvas(canvas)->clipRect(clipRect->left, clipRect->top, clipRect->right,
91                                          clipRect->bottom, SkClipOp::kIntersect);
92 }
93 
ACanvas_clipOutRect(ACanvas * canvas,const ARect * clipRect,bool)94 void 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 
ACanvas_drawRect(ACanvas * canvas,const ARect * rect,const APaint * paint)100 void 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 
ACanvas_drawBitmap(ACanvas * canvas,const ABitmap * bitmap,float left,float top,const APaint * paint)105 void 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));
109 }
110