1021693b967a2c5556dddd183eb0247df4079e1adStan Iliev/*
2021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * Copyright (C) 2016 The Android Open Source Project
3021693b967a2c5556dddd183eb0247df4079e1adStan Iliev *
4021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * Licensed under the Apache License, Version 2.0 (the "License");
5021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * you may not use this file except in compliance with the License.
6021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * You may obtain a copy of the License at
7021693b967a2c5556dddd183eb0247df4079e1adStan Iliev *
8021693b967a2c5556dddd183eb0247df4079e1adStan Iliev *      http://www.apache.org/licenses/LICENSE-2.0
9021693b967a2c5556dddd183eb0247df4079e1adStan Iliev *
10021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * Unless required by applicable law or agreed to in writing, software
11021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * distributed under the License is distributed on an "AS IS" BASIS,
12021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * See the License for the specific language governing permissions and
14021693b967a2c5556dddd183eb0247df4079e1adStan Iliev * limitations under the License.
15021693b967a2c5556dddd183eb0247df4079e1adStan Iliev */
16021693b967a2c5556dddd183eb0247df4079e1adStan Iliev
178cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel#include "GlLayer.h"
18021693b967a2c5556dddd183eb0247df4079e1adStan Iliev#include "LayerDrawable.h"
1945ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel#include "VkLayer.h"
2045ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel
21f87da67df90968adcddb2404ff20a41fe49a0315Derek Sollenberger#include "SkColorFilter.h"
2245ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel#include "SkSurface.h"
23021693b967a2c5556dddd183eb0247df4079e1adStan Iliev#include "gl/GrGLTypes.h"
24021693b967a2c5556dddd183eb0247df4079e1adStan Iliev
25021693b967a2c5556dddd183eb0247df4079e1adStan Ilievnamespace android {
26021693b967a2c5556dddd183eb0247df4079e1adStan Ilievnamespace uirenderer {
27021693b967a2c5556dddd183eb0247df4079e1adStan Ilievnamespace skiapipeline {
28021693b967a2c5556dddd183eb0247df4079e1adStan Iliev
29021693b967a2c5556dddd183eb0247df4079e1adStan Ilievvoid LayerDrawable::onDraw(SkCanvas* canvas) {
30c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    DrawLayer(canvas->getGrContext(), canvas, mLayer.get());
31c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger}
32c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger
33c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenbergerbool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer) {
34021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    // transform the matrix based on the layer
35021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    int saveCount = -1;
36c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    if (!layer->getTransform().isIdentity()) {
37021693b967a2c5556dddd183eb0247df4079e1adStan Iliev        saveCount = canvas->save();
38021693b967a2c5556dddd183eb0247df4079e1adStan Iliev        SkMatrix transform;
39c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger        layer->getTransform().copyTo(transform);
40021693b967a2c5556dddd183eb0247df4079e1adStan Iliev        canvas->concat(transform);
41021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    }
428cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel
4345ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel    sk_sp<SkImage> layerImage;
4445ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel    if (layer->getApi() == Layer::Api::OpenGL) {
4545ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        GlLayer* glLayer = static_cast<GlLayer*>(layer);
4645ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        GrGLTextureInfo externalTexture;
4745ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        externalTexture.fTarget = glLayer->getRenderTarget();
4845ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        externalTexture.fID = glLayer->getTextureId();
4945ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        GrBackendTextureDesc textureDescription;
5045ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        textureDescription.fWidth = glLayer->getWidth();
5145ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        textureDescription.fHeight = glLayer->getHeight();
5245ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        textureDescription.fConfig = kRGBA_8888_GrPixelConfig;
5345ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        textureDescription.fOrigin = kTopLeft_GrSurfaceOrigin;
5445ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        textureDescription.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
5545ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        layerImage = SkImage::MakeFromTexture(context, textureDescription);
5645ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel    } else {
5745ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        SkASSERT(layer->getApi() == Layer::Api::Vulkan);
5845ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        VkLayer* vkLayer = static_cast<VkLayer*>(layer);
5945ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        canvas->clear(SK_ColorGREEN);
6045ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel        layerImage = vkLayer->getImage();
6145ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel    }
6245ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel
63021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    if (layerImage) {
64021693b967a2c5556dddd183eb0247df4079e1adStan Iliev        SkPaint paint;
65c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger        paint.setAlpha(layer->getAlpha());
66c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger        paint.setBlendMode(layer->getMode());
67c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger        paint.setColorFilter(sk_ref_sp(layer->getColorFilter()));
68021693b967a2c5556dddd183eb0247df4079e1adStan Iliev        canvas->drawImage(layerImage, 0, 0, &paint);
69021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    }
70021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    // restore the original matrix
71021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    if (saveCount >= 0) {
72021693b967a2c5556dddd183eb0247df4079e1adStan Iliev        canvas->restoreToCount(saveCount);
73021693b967a2c5556dddd183eb0247df4079e1adStan Iliev    }
74c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger
75c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    return layerImage;
76021693b967a2c5556dddd183eb0247df4079e1adStan Iliev}
77021693b967a2c5556dddd183eb0247df4079e1adStan Iliev
78021693b967a2c5556dddd183eb0247df4079e1adStan Iliev}; // namespace skiapipeline
79021693b967a2c5556dddd183eb0247df4079e1adStan Iliev}; // namespace uirenderer
80021693b967a2c5556dddd183eb0247df4079e1adStan Iliev}; // namespace android
81