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