1 /*
2  * Copyright (C) 2016 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 "GlLayer.h"
18 #include "LayerDrawable.h"
19 #include "VkLayer.h"
20 
21 #include "SkColorFilter.h"
22 #include "SkSurface.h"
23 #include "gl/GrGLTypes.h"
24 
25 namespace android {
26 namespace uirenderer {
27 namespace skiapipeline {
28 
onDraw(SkCanvas * canvas)29 void LayerDrawable::onDraw(SkCanvas* canvas) {
30     DrawLayer(canvas->getGrContext(), canvas, mLayer.get());
31 }
32 
DrawLayer(GrContext * context,SkCanvas * canvas,Layer * layer)33 bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer) {
34     // transform the matrix based on the layer
35     int saveCount = -1;
36     if (!layer->getTransform().isIdentity()) {
37         saveCount = canvas->save();
38         SkMatrix transform;
39         layer->getTransform().copyTo(transform);
40         canvas->concat(transform);
41     }
42 
43     sk_sp<SkImage> layerImage;
44     if (layer->getApi() == Layer::Api::OpenGL) {
45         GlLayer* glLayer = static_cast<GlLayer*>(layer);
46         GrGLTextureInfo externalTexture;
47         externalTexture.fTarget = glLayer->getRenderTarget();
48         externalTexture.fID = glLayer->getTextureId();
49         GrBackendTextureDesc textureDescription;
50         textureDescription.fWidth = glLayer->getWidth();
51         textureDescription.fHeight = glLayer->getHeight();
52         textureDescription.fConfig = kRGBA_8888_GrPixelConfig;
53         textureDescription.fOrigin = kTopLeft_GrSurfaceOrigin;
54         textureDescription.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
55         layerImage = SkImage::MakeFromTexture(context, textureDescription);
56     } else {
57         SkASSERT(layer->getApi() == Layer::Api::Vulkan);
58         VkLayer* vkLayer = static_cast<VkLayer*>(layer);
59         canvas->clear(SK_ColorGREEN);
60         layerImage = vkLayer->getImage();
61     }
62 
63     if (layerImage) {
64         SkPaint paint;
65         paint.setAlpha(layer->getAlpha());
66         paint.setBlendMode(layer->getMode());
67         paint.setColorFilter(sk_ref_sp(layer->getColorFilter()));
68         canvas->drawImage(layerImage, 0, 0, &paint);
69     }
70     // restore the original matrix
71     if (saveCount >= 0) {
72         canvas->restoreToCount(saveCount);
73     }
74 
75     return layerImage;
76 }
77 
78 }; // namespace skiapipeline
79 }; // namespace uirenderer
80 }; // namespace android
81